Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include<stdio.h>
void main()
{ 
    int a=010;
    printf("\na=%d",a);
}

#include<stdio.h>
void main()
{ 
    int a=010;
    printf("\na=%o",a);
}

#include<stdio.h>
void main()
{
    int a=010;
    printf("\na=%x",a);
}



Output for first program:8
Output for second program:10
Output for the third program:8

Since I am a beginner, I would like to know the reason behind the difference in ouput when using different letters after % in printf(). The next I don't understand how the number in the variable doesn't print and other values get printed.

I don't understand how 010=8 for octal when it is equal to 2 in binary.
Please help me. Thanks.
Posted
Updated 3-Jun-15 7:49am
v3
Comments
CPallini 3-Jun-15 15:54pm    
Because, apparently, if you use a different format specifier then you get a different format.

you are setting the variable to the value 010 which is octal for the decimal value 8.
Case 1: print as decimal: 8
Case 2: print as octal: 010
Case 3: print as hexadecimal: 8

You need to go and study number systems to understand why they are different.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Jun-15 13:10pm    
5ed. I also answered, with some more detail — Solution 2.
—SA
CPallini 3-Jun-15 15:52pm    
5.
These are string representations in different formats: decimal, octal and hexadecimal: http://www.cplusplus.com/reference/cstdio/printf[^].

First of all, 010 in C++ is pretty much misleading: it actually means decimal 8. Please see: http://en.cppreference.com/w/cpp/language/integer_literal[^].

In a way, all the outputs are identical, only expressed in positional numeric notations with different base. You may easily understand it if you change your formats to "a = %d", "a = octal 0%o" and "a = 0x%x". :-)

—SA
 
Share this answer
 
v3
Comments
CPallini 3-Jun-15 15:53pm    
5.
Sergey Alexandrovich Kryukov 3-Jun-15 17:46pm    
Thank you, Carlo.
—SA
In all three programs, the variable a contains the number 8. By putting 0 at the start, you've used an octal literal[^].

  • In the first program, you output the number in decimal[^] (base 10) - the result is 8.
  • In the second program, you output the number in octal[^] (base 8) - the result is 10, since octal can only use digits 0 to 7.
  • In the third program, you output the number in hexadecimal[^] (base 16) - the result is 8.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Jun-15 13:17pm    
Sure, a 5.
—SA
Richard Deeming 3-Jun-15 13:20pm    
The site's obviously running a bit slow today - I'm sure those other solutions weren't there when I posted mine! :)
Sergey Alexandrovich Kryukov 3-Jun-15 13:32pm    
I know. That's why I could justifiably consider all three answers as "simultaneous" when I voted.
—SA
CPallini 3-Jun-15 15:53pm    
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