Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

How does int a=65; printf("%c", a); work in c language in GCC?

int a=65; 
printf("%c", a);

I tried it on GCC on Ubuntu I don't know the version. But it worked and I wish to know how? Because according to me the size of char is smaller than int and hence it shouldn't have been possible.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The printf function has the following declaration:

int printf(const char *format, ...);

The first argument must be a pointer to a char array, but any additional arguments can be of any type, so it's not a compiler error if a format specifier mismatches the parameter (although it is undefined behavior).

This still works however because of what %c expects. From the man page:

If no l modifier is present, the int argument is converted to an unsigned char, and the resulting character is written. If an l modifier is present, the wint_t (wide character) argument is converted to a multibyte sequence by a call to the wcrtomb(3) function, with a conversion state starting in the initial state, and the resulting multibyte string is written.

From the above passage, %c actually expects an int and converts it to an unsigned char for printing. So if that's the case why does passing an actual char work? That is because of integer promotions. Any integer type smaller than int is promoted to int anyplace an int can be used. Since printf is variadic it can't check the types of its arguments, so a char passed to printf will get promoted to int when the function is called.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...