Sep
26

Concept of Octal literal in C

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
 

JIGAR PATEL

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 JigAr Patel

More PostsWebsiteTwitterFacebook

You may like to read this also....

Post comment

*

Follow us on Twitter! Follow us on Twitter!

Search in this website

our sponsors

latest comments

Find us on Facebook

Top Authors

36 posts
saurabh
22 posts
10 posts

Find us on stackoverflow

Polls

Tell us who you are

View Results

Loading ... Loading ...

My Bookmarks

Sponsers Link