What's the logic behind addition of 0101 with 5 in C? [duplicate]












4
















This question already has an answer here:




  • What does it mean when a numeric constant in C/C++ is prefixed with a 0?

    7 answers




One of my friends asked me the output of this code and I just got shocked after running this code. The output of this code is 70. Please explain why?



#include <stdio.h>
int main()

{
int var = 0101;
var = var+5;
printf("%d",var);
return 0;
}









share|improve this question















marked as duplicate by arghtype, jwodder, Boann, Blackhole, StoryTeller c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Feb 6 at 19:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 2





    Constants starting with 0 are in octal base. 0101 is the same as 65.

    – Eugene Sh.
    Feb 6 at 17:03








  • 1





    I believe the leading 0 of var is actually , making it octal 101 = 65.

    – RJM
    Feb 6 at 17:03













  • Simpler demonstration: printf("%d", 0101);

    – Boann
    Feb 6 at 19:00






  • 2





    Q: Why do C programmers confuse Halloween and Christmas? A: Because Oct 31 = Dec 25.

    – Mason Wheeler
    Feb 6 at 19:04
















4
















This question already has an answer here:




  • What does it mean when a numeric constant in C/C++ is prefixed with a 0?

    7 answers




One of my friends asked me the output of this code and I just got shocked after running this code. The output of this code is 70. Please explain why?



#include <stdio.h>
int main()

{
int var = 0101;
var = var+5;
printf("%d",var);
return 0;
}









share|improve this question















marked as duplicate by arghtype, jwodder, Boann, Blackhole, StoryTeller c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Feb 6 at 19:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 2





    Constants starting with 0 are in octal base. 0101 is the same as 65.

    – Eugene Sh.
    Feb 6 at 17:03








  • 1





    I believe the leading 0 of var is actually , making it octal 101 = 65.

    – RJM
    Feb 6 at 17:03













  • Simpler demonstration: printf("%d", 0101);

    – Boann
    Feb 6 at 19:00






  • 2





    Q: Why do C programmers confuse Halloween and Christmas? A: Because Oct 31 = Dec 25.

    – Mason Wheeler
    Feb 6 at 19:04














4












4








4









This question already has an answer here:




  • What does it mean when a numeric constant in C/C++ is prefixed with a 0?

    7 answers




One of my friends asked me the output of this code and I just got shocked after running this code. The output of this code is 70. Please explain why?



#include <stdio.h>
int main()

{
int var = 0101;
var = var+5;
printf("%d",var);
return 0;
}









share|improve this question

















This question already has an answer here:




  • What does it mean when a numeric constant in C/C++ is prefixed with a 0?

    7 answers




One of my friends asked me the output of this code and I just got shocked after running this code. The output of this code is 70. Please explain why?



#include <stdio.h>
int main()

{
int var = 0101;
var = var+5;
printf("%d",var);
return 0;
}




This question already has an answer here:




  • What does it mean when a numeric constant in C/C++ is prefixed with a 0?

    7 answers








c binary






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 6 at 17:06









Govind Parmar

12.3k53463




12.3k53463










asked Feb 6 at 17:01









Uddesh_jainUddesh_jain

27124




27124




marked as duplicate by arghtype, jwodder, Boann, Blackhole, StoryTeller c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Feb 6 at 19:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by arghtype, jwodder, Boann, Blackhole, StoryTeller c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Feb 6 at 19:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2





    Constants starting with 0 are in octal base. 0101 is the same as 65.

    – Eugene Sh.
    Feb 6 at 17:03








  • 1





    I believe the leading 0 of var is actually , making it octal 101 = 65.

    – RJM
    Feb 6 at 17:03













  • Simpler demonstration: printf("%d", 0101);

    – Boann
    Feb 6 at 19:00






  • 2





    Q: Why do C programmers confuse Halloween and Christmas? A: Because Oct 31 = Dec 25.

    – Mason Wheeler
    Feb 6 at 19:04














  • 2





    Constants starting with 0 are in octal base. 0101 is the same as 65.

    – Eugene Sh.
    Feb 6 at 17:03








  • 1





    I believe the leading 0 of var is actually , making it octal 101 = 65.

    – RJM
    Feb 6 at 17:03













  • Simpler demonstration: printf("%d", 0101);

    – Boann
    Feb 6 at 19:00






  • 2





    Q: Why do C programmers confuse Halloween and Christmas? A: Because Oct 31 = Dec 25.

    – Mason Wheeler
    Feb 6 at 19:04








2




2





Constants starting with 0 are in octal base. 0101 is the same as 65.

– Eugene Sh.
Feb 6 at 17:03







Constants starting with 0 are in octal base. 0101 is the same as 65.

– Eugene Sh.
Feb 6 at 17:03






1




1





I believe the leading 0 of var is actually , making it octal 101 = 65.

– RJM
Feb 6 at 17:03







I believe the leading 0 of var is actually , making it octal 101 = 65.

– RJM
Feb 6 at 17:03















Simpler demonstration: printf("%d", 0101);

– Boann
Feb 6 at 19:00





Simpler demonstration: printf("%d", 0101);

– Boann
Feb 6 at 19:00




2




2





Q: Why do C programmers confuse Halloween and Christmas? A: Because Oct 31 = Dec 25.

– Mason Wheeler
Feb 6 at 19:04





Q: Why do C programmers confuse Halloween and Christmas? A: Because Oct 31 = Dec 25.

– Mason Wheeler
Feb 6 at 19:04












4 Answers
4






active

oldest

votes


















11














The C Standard dictates that a numeric constant beginning with 0 is an octal constant (i.e. base-8) in § 6.4.4.1 (Integer constants).



The value 101 in base 8 is 65 in base 10, so adding 5 to it (obviously) produces 70 in base 10.



Try changing your format specifier in printf to "%o" to observe the octal representation of var.






share|improve this answer

































    5














    It is because of Integer Literals. A number with leading 0 denoted that the number is an octal number. You can also use 0b for denoting binary number, for hexadecimal number it is 0x or 0X. You don't need to write any thing for decimal. See the code bellow.



    #include<stdio.h>

    int main()
    {
    int binary = 0b10;
    int octal=010;
    int decimal = 10;
    int hexa = 0x10;
    printf("%d %d %d %dn", octal, decimal, hexa, binary);
    }


    For more information visit tutorialspoint.






    share|improve this answer



















    • 1





      I don't believe that the syntax 0bnnn is currently supported by the C standard, though many compilers have it as an extension

      – Govind Parmar
      Feb 6 at 17:40



















    4














    Not shocking at all. C11 Standard - 6.4.4.1 Integer constants(p3) provides:




    "An octal constant consists of the prefix 0 optionally followed by a
    sequence of the digits 0 through 7 only."




    Some compilers, such as gcc, provide extension for specifying binary constants, e.g. GCC Manual - 6.64 Binary Constants using the ‘0b’ Prefix But note, this is a non-standard extension.



    Combining both in your example would give:



    #include <stdio.h>

    int main (void) {

    int var = 0101,
    bar = 0b0101;
    var = var + 5;
    bar = bar + 5;
    printf ("var: %dnbar: %dn", var, bar);

    return 0;
    }


    Example Use/Output



    $ ./bin/octbin
    var: 70
    bar: 10





    share|improve this answer































      1














      Inicially var is in octal numeric system, so var=0101 is equal to 001000001 in binary system or equal to 65 in decimal system.



      for example in this code you can show 65 as the var inicial value.



      #include <stdio.h>
      int main()

      {
      int var = 0101;
      printf("initial value. var=%on",var);
      var = var+5;

      printf("result of var+5. var=%dn",var);
      printf("%dn",var);
      return 0;
      }


      You'll get this output:



      initial value. var=65
      result of var+5. var=70
      70





      share|improve this answer






























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        11














        The C Standard dictates that a numeric constant beginning with 0 is an octal constant (i.e. base-8) in § 6.4.4.1 (Integer constants).



        The value 101 in base 8 is 65 in base 10, so adding 5 to it (obviously) produces 70 in base 10.



        Try changing your format specifier in printf to "%o" to observe the octal representation of var.






        share|improve this answer






























          11














          The C Standard dictates that a numeric constant beginning with 0 is an octal constant (i.e. base-8) in § 6.4.4.1 (Integer constants).



          The value 101 in base 8 is 65 in base 10, so adding 5 to it (obviously) produces 70 in base 10.



          Try changing your format specifier in printf to "%o" to observe the octal representation of var.






          share|improve this answer




























            11












            11








            11







            The C Standard dictates that a numeric constant beginning with 0 is an octal constant (i.e. base-8) in § 6.4.4.1 (Integer constants).



            The value 101 in base 8 is 65 in base 10, so adding 5 to it (obviously) produces 70 in base 10.



            Try changing your format specifier in printf to "%o" to observe the octal representation of var.






            share|improve this answer















            The C Standard dictates that a numeric constant beginning with 0 is an octal constant (i.e. base-8) in § 6.4.4.1 (Integer constants).



            The value 101 in base 8 is 65 in base 10, so adding 5 to it (obviously) produces 70 in base 10.



            Try changing your format specifier in printf to "%o" to observe the octal representation of var.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 6 at 18:06

























            answered Feb 6 at 17:05









            Govind ParmarGovind Parmar

            12.3k53463




            12.3k53463

























                5














                It is because of Integer Literals. A number with leading 0 denoted that the number is an octal number. You can also use 0b for denoting binary number, for hexadecimal number it is 0x or 0X. You don't need to write any thing for decimal. See the code bellow.



                #include<stdio.h>

                int main()
                {
                int binary = 0b10;
                int octal=010;
                int decimal = 10;
                int hexa = 0x10;
                printf("%d %d %d %dn", octal, decimal, hexa, binary);
                }


                For more information visit tutorialspoint.






                share|improve this answer



















                • 1





                  I don't believe that the syntax 0bnnn is currently supported by the C standard, though many compilers have it as an extension

                  – Govind Parmar
                  Feb 6 at 17:40
















                5














                It is because of Integer Literals. A number with leading 0 denoted that the number is an octal number. You can also use 0b for denoting binary number, for hexadecimal number it is 0x or 0X. You don't need to write any thing for decimal. See the code bellow.



                #include<stdio.h>

                int main()
                {
                int binary = 0b10;
                int octal=010;
                int decimal = 10;
                int hexa = 0x10;
                printf("%d %d %d %dn", octal, decimal, hexa, binary);
                }


                For more information visit tutorialspoint.






                share|improve this answer



















                • 1





                  I don't believe that the syntax 0bnnn is currently supported by the C standard, though many compilers have it as an extension

                  – Govind Parmar
                  Feb 6 at 17:40














                5












                5








                5







                It is because of Integer Literals. A number with leading 0 denoted that the number is an octal number. You can also use 0b for denoting binary number, for hexadecimal number it is 0x or 0X. You don't need to write any thing for decimal. See the code bellow.



                #include<stdio.h>

                int main()
                {
                int binary = 0b10;
                int octal=010;
                int decimal = 10;
                int hexa = 0x10;
                printf("%d %d %d %dn", octal, decimal, hexa, binary);
                }


                For more information visit tutorialspoint.






                share|improve this answer













                It is because of Integer Literals. A number with leading 0 denoted that the number is an octal number. You can also use 0b for denoting binary number, for hexadecimal number it is 0x or 0X. You don't need to write any thing for decimal. See the code bellow.



                #include<stdio.h>

                int main()
                {
                int binary = 0b10;
                int octal=010;
                int decimal = 10;
                int hexa = 0x10;
                printf("%d %d %d %dn", octal, decimal, hexa, binary);
                }


                For more information visit tutorialspoint.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 6 at 17:25









                Niloy RashidNiloy Rashid

                1517




                1517








                • 1





                  I don't believe that the syntax 0bnnn is currently supported by the C standard, though many compilers have it as an extension

                  – Govind Parmar
                  Feb 6 at 17:40














                • 1





                  I don't believe that the syntax 0bnnn is currently supported by the C standard, though many compilers have it as an extension

                  – Govind Parmar
                  Feb 6 at 17:40








                1




                1





                I don't believe that the syntax 0bnnn is currently supported by the C standard, though many compilers have it as an extension

                – Govind Parmar
                Feb 6 at 17:40





                I don't believe that the syntax 0bnnn is currently supported by the C standard, though many compilers have it as an extension

                – Govind Parmar
                Feb 6 at 17:40











                4














                Not shocking at all. C11 Standard - 6.4.4.1 Integer constants(p3) provides:




                "An octal constant consists of the prefix 0 optionally followed by a
                sequence of the digits 0 through 7 only."




                Some compilers, such as gcc, provide extension for specifying binary constants, e.g. GCC Manual - 6.64 Binary Constants using the ‘0b’ Prefix But note, this is a non-standard extension.



                Combining both in your example would give:



                #include <stdio.h>

                int main (void) {

                int var = 0101,
                bar = 0b0101;
                var = var + 5;
                bar = bar + 5;
                printf ("var: %dnbar: %dn", var, bar);

                return 0;
                }


                Example Use/Output



                $ ./bin/octbin
                var: 70
                bar: 10





                share|improve this answer




























                  4














                  Not shocking at all. C11 Standard - 6.4.4.1 Integer constants(p3) provides:




                  "An octal constant consists of the prefix 0 optionally followed by a
                  sequence of the digits 0 through 7 only."




                  Some compilers, such as gcc, provide extension for specifying binary constants, e.g. GCC Manual - 6.64 Binary Constants using the ‘0b’ Prefix But note, this is a non-standard extension.



                  Combining both in your example would give:



                  #include <stdio.h>

                  int main (void) {

                  int var = 0101,
                  bar = 0b0101;
                  var = var + 5;
                  bar = bar + 5;
                  printf ("var: %dnbar: %dn", var, bar);

                  return 0;
                  }


                  Example Use/Output



                  $ ./bin/octbin
                  var: 70
                  bar: 10





                  share|improve this answer


























                    4












                    4








                    4







                    Not shocking at all. C11 Standard - 6.4.4.1 Integer constants(p3) provides:




                    "An octal constant consists of the prefix 0 optionally followed by a
                    sequence of the digits 0 through 7 only."




                    Some compilers, such as gcc, provide extension for specifying binary constants, e.g. GCC Manual - 6.64 Binary Constants using the ‘0b’ Prefix But note, this is a non-standard extension.



                    Combining both in your example would give:



                    #include <stdio.h>

                    int main (void) {

                    int var = 0101,
                    bar = 0b0101;
                    var = var + 5;
                    bar = bar + 5;
                    printf ("var: %dnbar: %dn", var, bar);

                    return 0;
                    }


                    Example Use/Output



                    $ ./bin/octbin
                    var: 70
                    bar: 10





                    share|improve this answer













                    Not shocking at all. C11 Standard - 6.4.4.1 Integer constants(p3) provides:




                    "An octal constant consists of the prefix 0 optionally followed by a
                    sequence of the digits 0 through 7 only."




                    Some compilers, such as gcc, provide extension for specifying binary constants, e.g. GCC Manual - 6.64 Binary Constants using the ‘0b’ Prefix But note, this is a non-standard extension.



                    Combining both in your example would give:



                    #include <stdio.h>

                    int main (void) {

                    int var = 0101,
                    bar = 0b0101;
                    var = var + 5;
                    bar = bar + 5;
                    printf ("var: %dnbar: %dn", var, bar);

                    return 0;
                    }


                    Example Use/Output



                    $ ./bin/octbin
                    var: 70
                    bar: 10






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Feb 6 at 17:30









                    David C. RankinDavid C. Rankin

                    42.5k32949




                    42.5k32949























                        1














                        Inicially var is in octal numeric system, so var=0101 is equal to 001000001 in binary system or equal to 65 in decimal system.



                        for example in this code you can show 65 as the var inicial value.



                        #include <stdio.h>
                        int main()

                        {
                        int var = 0101;
                        printf("initial value. var=%on",var);
                        var = var+5;

                        printf("result of var+5. var=%dn",var);
                        printf("%dn",var);
                        return 0;
                        }


                        You'll get this output:



                        initial value. var=65
                        result of var+5. var=70
                        70





                        share|improve this answer




























                          1














                          Inicially var is in octal numeric system, so var=0101 is equal to 001000001 in binary system or equal to 65 in decimal system.



                          for example in this code you can show 65 as the var inicial value.



                          #include <stdio.h>
                          int main()

                          {
                          int var = 0101;
                          printf("initial value. var=%on",var);
                          var = var+5;

                          printf("result of var+5. var=%dn",var);
                          printf("%dn",var);
                          return 0;
                          }


                          You'll get this output:



                          initial value. var=65
                          result of var+5. var=70
                          70





                          share|improve this answer


























                            1












                            1








                            1







                            Inicially var is in octal numeric system, so var=0101 is equal to 001000001 in binary system or equal to 65 in decimal system.



                            for example in this code you can show 65 as the var inicial value.



                            #include <stdio.h>
                            int main()

                            {
                            int var = 0101;
                            printf("initial value. var=%on",var);
                            var = var+5;

                            printf("result of var+5. var=%dn",var);
                            printf("%dn",var);
                            return 0;
                            }


                            You'll get this output:



                            initial value. var=65
                            result of var+5. var=70
                            70





                            share|improve this answer













                            Inicially var is in octal numeric system, so var=0101 is equal to 001000001 in binary system or equal to 65 in decimal system.



                            for example in this code you can show 65 as the var inicial value.



                            #include <stdio.h>
                            int main()

                            {
                            int var = 0101;
                            printf("initial value. var=%on",var);
                            var = var+5;

                            printf("result of var+5. var=%dn",var);
                            printf("%dn",var);
                            return 0;
                            }


                            You'll get this output:



                            initial value. var=65
                            result of var+5. var=70
                            70






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Feb 6 at 17:55









                            Rogelio PrietoRogelio Prieto

                            514




                            514















                                Popular posts from this blog

                                Aardman Animations

                                Are they similar matrix

                                “minimization” problem in Euclidean space related to orthonormal basis