Printf() in c printing 0's for extra format specifiers [closed]
#include<stdio.h>
int main()
{
int a,*b,**c,***d,****e;
a=10;
b=&a;
c=&b;
d=&c;
e=&d;
printf("\na=%d b=%u c=%u d=%u e=%u",a,b,c,d,e);
printf("\n%d %d %d %d %d",a,a+*b,**c+***d+****e);
return 0;
}
I could not edit this post... All the options t开发者_如何学JAVAo do so are not visible to my browser.I meant to ask why the compiler didnt warn me and is giving me the output as 0 0 for the extra format specifiers.
You have not provided enough parameters to your second call to printf
and have invoked undefined behaviour. Please refrain from doing this. Your compiler should warn about this if you configure its warnings appropriately,
What do you expect it to print when given five conversion specifications but only three arguments?
The C standard says, in 7.19.6.1/2
If there are insufficient arguments for the format, the behavior is undefined.
In your case, the program happend to print zeroes. In my case, it printed something else.
EDIT in response to the question "why?": Most compilers do warn about this error:
gcc says warning: format ‘%d’ expects a matching ‘int’ argument
clang says warning: more '%' conversions than data arguments
icc says warning #267: the format string requires additional arguments
However, there is no requirement that they must diagnose this. Undefined behavior is just that, undefined. Anything can happen.
精彩评论