C output

Why are the ouputs different?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<#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. Please help me. Thanks.
%d is for int.
%o is for octal.
%x is for hex.

In your code a = 010 thats an octal vale which would translate to an 8 if it was an int and same for hex
Last edited on
 I would like to know the reason behind the difference in ouput when using different letters after % in printf()

they are the placeholders to print a variable or a certain value


    %d - int (same as %i)
    %ld - long int (same as %li)
    %f - float
    %lf - double
    %c - char
    %s - string
    %x - hexadecimal
    -google



printf("\na=%d",a);
when you put %d you are saying that this place is for integer
Thanks to all for your useful answers. I somewhat get it. But, I would like to know if I can represent binary values when I can represent all other types. Thanks.
Topic archived. No new replies allowed.