I am unable to understand the output produced by the following code:
#include <stdio.h>
int main()
{
int var = 010;
printf("%d", var);
}
The output of the above code is 8
.
I am unable to understand the output produced by the following code:
#include <stdio.h>
int main()
{
int var = 010;
printf("%d", var);
}
The output of the above code is 8
.
A leading 0
introduces an octal constant, so 010
is octal which is 8 in decimal. If you want binary, write 0b010
(which is 2 in decimal).
010
is an integer constant (i.e. literal), encoded in octal:
001 == 1
002 == 2
...
007 == 7
010 == 8
When you call printf
with a format specifier %d
, it prints the value of the given signed integer encoded in decimal, and therefore, you will see the character 8
written to the output.