What's the logic behind addition of 0101 with 5 in C? [duplicate]
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;
}
c binary
marked as duplicate by arghtype, jwodder, Boann, Blackhole, StoryTeller
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.
add a comment |
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;
}
c binary
marked as duplicate by arghtype, jwodder, Boann, Blackhole, StoryTeller
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 with0
are in octal base.0101
is the same as65
.
– 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
add a comment |
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;
}
c binary
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
c binary
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
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
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 with0
are in octal base.0101
is the same as65
.
– 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
add a comment |
2
Constants starting with0
are in octal base.0101
is the same as65
.
– 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
add a comment |
4 Answers
4
active
oldest
votes
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
.
add a comment |
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.
1
I don't believe that the syntax0bnnn
is currently supported by the C standard, though many compilers have it as an extension
– Govind Parmar
Feb 6 at 17:40
add a comment |
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
add a comment |
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
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
.
add a comment |
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
.
add a comment |
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
.
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
.
edited Feb 6 at 18:06
answered Feb 6 at 17:05
Govind ParmarGovind Parmar
12.3k53463
12.3k53463
add a comment |
add a comment |
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.
1
I don't believe that the syntax0bnnn
is currently supported by the C standard, though many compilers have it as an extension
– Govind Parmar
Feb 6 at 17:40
add a comment |
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.
1
I don't believe that the syntax0bnnn
is currently supported by the C standard, though many compilers have it as an extension
– Govind Parmar
Feb 6 at 17:40
add a comment |
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.
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.
answered Feb 6 at 17:25
Niloy RashidNiloy Rashid
1517
1517
1
I don't believe that the syntax0bnnn
is currently supported by the C standard, though many compilers have it as an extension
– Govind Parmar
Feb 6 at 17:40
add a comment |
1
I don't believe that the syntax0bnnn
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Feb 6 at 17:30
David C. RankinDavid C. Rankin
42.5k32949
42.5k32949
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Feb 6 at 17:55
Rogelio PrietoRogelio Prieto
514
514
add a comment |
add a comment |
2
Constants starting with
0
are in octal base.0101
is the same as65
.– 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