Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C
#include<stdio.h>
#include<conio.h>
void main()
{
int a=16,b=012,c=054;
printf("a=%d b=%d c=%d",a,b,c);

}


What I have tried:

why the output changes what is the conversion...
Posted
Updated 23-Oct-17 20:59pm
v2

1 solution

When you prefix a number with 0 in C it assumes that the number you are inputting is no long in decimal (base 10, the one we normally use in the real world) but instead in octal (base 8, it runs 0, 1, ... 7, 10, 11, ... 17, 20, 21, ...)
So this:
int a=16,b=012,c=054;
Creates three variables and assignes values to them:
a    16   deciml, value 16
b   012   octal,  value 10 in decimal
c   054   octal,  value 44 in decimal

You can also prefix number with "0x" or "0X" to enter hexadecimal (or just "hex") numbers
- which are a lot more common these days than octal.

See here: Octal - Wikipedia[^]
And here: Hexadecimal - Wikipedia[^]
 
Share this answer
 
Comments
CPallini 24-Oct-17 2:59am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900