The following gives this error in GCC: pasting "func_x" and "(" does not give a valid preprocessing token
#include <stdio.h>
#define LOG_FUNC(fname) printf( #fname " %d\n", fname ## ())
int func_x(){
return 0;
}
int main(){
LOG_FUNC(func_x);
return 0;
}
However, when I hover the mouse on top of the function, it shows that the macro expands to following expression that works without a problem
printf("func_x" " %d\n", func_x())
Is it because the parenthesis is not allowed in pre processor string concatenation?
CodePudding user response:
If you want to get your code to compile, change LOG_FUNC
to:
#define LOG_FUNC(fname) printf( #fname " %d\n", fname())
If you want to concatenate two strings together, just write them next to each other, separated by whitespace. It is OK if they contain parens. You don't have to use the preprocessor for this, but you can:
#include <stdio.h>
#define MY_STR(f) f "(\n"
int main() {
puts(MY_STR("hi")); // outputs "hi("
return 0;
}