why is output 9

+1 vote
asked Feb 6, 2019 by Hritik
#include<stdio.h>
int main (void)
{
int var=010;
var=var+1;
printf("%d",var);
return 0;
}

5 Answers

+1 vote
answered Feb 6, 2019 by Florentin (190 points)

Hi

In C , C++ , Objective C and related languages a 0 prefix signifies an octal literal constant, so 010 = 8 in decimal. 0 before the number means it's in octal notation. So since octal uses a base of 8 , 010 would equal 8 .

So put 10 instead of 010 and it will show 11

0 votes
answered Feb 6, 2019 by chetan gupta
It is because, prefix 0 to 10 means octal numbers of which base is 7

i n t     v a r   =   0 1 0 ;

                            ^

                    Compiler understand it as a octal number

try to use     0x for hexadecimal   and 0b  for binary you will get clear.
0 votes
answered Feb 7, 2019 by anonymous

shahid imam

cxchhokm

0 votes
answered Feb 7, 2019 by sar rajendran (180 points)
var=010  -------->  decimal value is 8

so

var=8+1

var=9
0 votes
answered Feb 7, 2019 by sar rajendran (180 points)
var=010   -----------> decimal value of 010 is 8

so

var=8+1

var=9
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...