I have the following program.
#include<stdio.h>
int main()
{
char a='b';
int b=11299;
char d[4]="abc";
printf("value of a is %d\n",a);
printf("value of b is %c\n",b);
printf("value of c is %d\n",*d);
char *c=d;
c=c+1;
printf("c is %d\n",*c);
}
I am little confused with %d format specifier. I was thinking that it would print 4 bytes of data. But from the above program (first and last printf) it is evident that it prints only one byte when a char parameter is used. Why does %d print only one byte? How does it know how many bytes to print?
printf
format specifiers, and contains links to more detail, such as width. The plain%d
specifier prints the given argument asint
using as many digits as necessary.a
is being passed toprintf()
it gets promoted toint
by the compiler.char
is promoted toint
.%d
, it expects and prints anint
value. Similar for%c
(well,char
actually is a byte, so ...)