Generating labels in macro preprocessing in C
How to write a macro which calls goto END_ label?
For ex:
#define MY_MACRO() \
//How to define goto END_##function_name label??
my_func开发者_如何学JAVAtion()
{
MY_MACRO();
END_my_function:
return;
}
The MY_MACRO should simply replace with the line
goto END_my_function;
I don't think it can be done. Even though some compilers define __FUNCTION__
or __func__
, these are not expanded in macros.
However, note that you don't need a separate label for each function: you can use END
for all functions and simply write goto END
.
The preprocessor does not know what function it is in. But you can get close -- you'll have to pass the function name to this macro
#include <stdio.h>
#define GOTO_END(f) goto f ## _end
void foo(void)
{
printf("At the beginning.\n");
GOTO_END(foo);
printf("You shouldn't see this.\n");
foo_end:
printf("At the end.\n");
return;
}
int main()
{
foo();
}
Use token concatenation.
#define MY_MACRO( function_name ) END_ ## function_name
精彩评论