Concept of Octal literal in C

By -

One day while debugging  one code i got one interesting things ..let me explain you by bellow’s  example.

#include<stdio.h>
main()
{
int a=010;
printf("a is %d",a);
}

if you think output of this program will

“a  is  10”

then your are missing something….


the real output of this program is

“a  is  8”

REASON :

Actually in c and c ++ language 0 (zero) is the octel literal so when you wrie

a = 010 ;

then its Octel representation of  8.

Any integer value starting with ‘0’ is an octal value. I.e.: 01 is octal 1, 010 is octal 10, which is decimal 8, and 0 is octal 0 (which is decimal, and any other, 0).

As per the C++ Standard:

2.14.2 Integer literals [lex.icon]

integer-literal:  
    decimal-literal integer-suffixopt  
    octal-literal integer-suffixopt  
    hexadecimal-literal integer-suffixopt  
decimal-literal:  
    nonzero-digit  
    decimal-literal digit  
octal-literal:  
    0                           
    octal-literal octal-digit

hey I am an Artist who love to write code...! Well I am an EC graduate From Ganpat University and now i am working as Embedded software engineer in one private firm.. find me at here Jeegar Patel

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA Image

*