1

I could not know why it happen! Want to know the reason.

{
int i=01;
printf("%d\n",i);
}
output: 1

but

{
int i=011;
printf("%d\n",i);
}
output: 9

Does anybody have the answer?

3 Answers 3

11

011 is an octal constant. 11 (b8) = 9 (b10).

C11 (n1570), § 6.4.4.1 Integer constants
An octal constant consists of the prefix 0 optionally followed by a sequence of the digits 0 through 7 only.

Sign up to request clarification or add additional context in comments.

4 Comments

why it will print octal number where as I have given a decimal integer value!
Because your numeric constant starts with a 0. Read the quotation.
@Iqbal Yes, of course, 1*8 + 2*1 = 10. If you start your literals with a 0, it's an octal literal. Try using the %o format in printf.
Read the quotation ;) prefix 0 for an octal constant (base 8) (and prefix 0x or 0X or simply x or X for hexadecimal base 16)
4

011 = Octal, (1*8)+1=9 ........................

Comments

-1
The numbers which are preceded by 0 is called octal numbers in c programming .
to evaluate such an expression we simply follow a conversion rule of converting octal to decimal number system
For conversion the following steps are  to be proceed
such as 011
here 0 indicate the number is octal 
and we are require to convert 11 which is (base 8) to decimal (base 10)

11= 1x8^1+1x8^0
   =8+1
   =9

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.