I am facing a problem with understanding the use of macro function calls from within a printf() statement.
I have the code below :
#include<stdio.h>
#define f(g,h) g##h
main()
{
printf("%d",f(100,10));
}
This code outputs "10010" as the answer.
I have learned that macro function call simply copy pastes the macro function code in place of the call with the arguments replaced.
So the code should be like :
#include<stdio.h>
#define f(g,h) g##h
main()
{
printf("%d",100##10);
}
But when i executed the above code separately with substituted macro,i get a compilation error.
So how does the first code gives 10010 as the answer while the second code gives a compilation error?
The preprocessor concatenation operator ##
is done before the macro is replaced. It can only be used in macro bodies.
Operator ##
has speacial meaning for preprocessor, it's a token-paste operator which 'glues' two tokens together. So in your case, g
and h
are 'glued' together, resulting in new token - int literal 10010
.
在宏中有一些像##
这样的特殊字符会改变规则“只是替换文本”。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.