Is this valid C code:
foo.h:
#ifndef FOO_H_
#define FOO_H_
#define FUNC(var) \
do \
{ \
//something \
}while(0) \
#endif
foo.c:
#include <stdio.h>
#include "foo.h"
FUNC(5); //How is it possible to call the function-like macro from here?
int main(void)
{
//do something
return 0;
}
I have some (working) code that is arranged like this, and in global scope calling a function-like macro. How is this possible?
I have some (working) code that is arranged like this, and in global scope calling a function-like macro. How is this possible?
There is nothing inherently wrong with a function-like macro being invoked at file scope. It's all a question of the macro's replacement text, in light of its arguments. The function-like macro you present in the question cannot be invoked at file scope because it expands to a do
/ while
statement, and those must appear inside functions. Whatever example you are thinking of that works cannot have the same kind of replacement text.
Here's an example demonstrating a function-like macro that can be expanded at file scope:
#include <stdio.h>
#define DECL_INT(v,i) int v = (i)
DECL_INT(var, 42);
int main(void) {
printf("%d\n", var);
}
That means exactly the same thing as ...
#include <stdio.h>
int var = (42);
int main(void) {
printf("%d\n", var);
}