I could not understand how the below program code outputs that value.Please help me to understand.
#include<开发者_开发百科;stdio.h>
char*s="char*s=%c%s%c;main(){printf(s,34,s,34);}";
int main()
{
printf(s,34,s,34);
return 0;
}
output:
char*s="char*s=%c%s%c;main(){printf(s,34,s,34);}";main(){printf(s,34,s,34);}
It isn't actually a macro in use here. It is just a simple call to printf. The first parameter to printf is the format string. In this case it is the value defined in the global variable s
. The format characters %c%s%c
are supplied by parameters 34,s,34
". So the string is just printed in its entirety because of the %s
format character. And the two 34 values are printed as double quote characters (via %c
).
Your printf
statement is effectively equivalent to:
printf("char*s=%c%s%c;main(){printf(s,34,s,34);}", 34, s, 34);
^ ^ ^
I've marked the conversion specifiers with ^
. These get replaced with, respectively:
"
- the ASCII character corresponding to 34- the contents of
*s
"
- the ASCII character corresponding to 34
You are inserting as an argument your format string, so the output is correct. The %s is replaced with the actual format. PS: Where are macros?
精彩评论