Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I used the following code
C++
main()
{
	int i=101;
	printf("%X",i);
}

to print the hexadecimal value it prints 65 correctly but when I add an extra 0 to the left i.e
C++
int i=0101;

the output changes to 41, when you take a double look it is 65 in hexadecimal, why is that why the double conversion?

What I have tried:

the code was run for both the integer values i.e i=0101 and i=101;
Posted
Updated 7-Oct-16 7:07am
v3

Adding the leading zero makes it look like an octal representation.
 
Share this answer
 
Comments
Erebus_22 7-Oct-16 12:32pm    
So what about 0111 which gives output as 49
Erebus_22 7-Oct-16 12:35pm    
thanks I get it
jeron1 7-Oct-16 12:40pm    
good!
Quote:
C++
int i=0101;
The leading 0 means octal (base 8), a leading 0b means binary (base 2) and a leading 0x means hexadecimal (base 16).

I guess a lecture of the basics will not harm.

Here is links to references books on C and C++ by the authors of the languages. The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]
 
Share this answer
 
v3

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