2
votes

For some imperscrutable reason, in my code I have something like:

#define pippo(x) printf("%d",x)
...
... many lines down in the code
...
int pippo = 0;

The same identifier pippo has been used for both a function-like macro and a variable name! Beside the confusion this could arise in the poor maintainer, I was wondering if this is legal by the standard.

Both C99 and C11 (in 6.10.3.) say:

10 [...] Each subsequent instance of the function-like macro name followed by a ( as the next preprocessing token introduces the sequence of preprocessing tokens that is replaced by the replacement list in the definition [...]

They don't say what happens if the function-like macro name is not followed by a '(' and I'm worried that some compiler might consider that this is an error (or might just emit a warning).

Am I too much of a worrywart?

2
Why do you worry if it compiles? But it is definitely better to fix. - Eugene Sh.
Good new word, that: "imperscrutable!" (I'd vote to make you an honorary Glaswegian.) - Adrian Mole
@EugeneSh.: One worries even if something compiles because there are bugs that do not prevent compilation and because something may compile in one compiler and not another. - Eric Postpischil

2 Answers

6
votes

Instances of the name of a function-like macro that are not followed by ( are not replaced.

Using names thusly is not a violation of constraints in the C standard. The standard even gives an example of using this behavior. C 2018 7.1.4 1, discussing standard library functions and their potential implementations as function-like macros (in addition to a definition as a function), says:

… Any macro definition of a function can be suppressed locally by enclosing the name of the function in parentheses, because the name is then not followed by the left parenthesis that indicates expansion of a macro function name. For the same syntactic reason, it is permitted to take the address of a library function even if it is also defined as a macro…

A compiler could give a warning (although it would likely want to suppress this warning when the macro name is a library function used as the C standard suggests, above), but neither GCC 9.2 nor Clang 11.0.0 do, even with all warnings enabled.

3
votes

5.1.1.2, point 4, specifies how the preprocessor is "invoked" if you will:

Preprocessing directives are executed, macro invocations are expanded, and _Pragma unary operator expressions are executed. [...] All preprocessing directives are then deleted.

So this implies that anything that is not touched by the preprocessor is left alone, including any pippo that is not followed by (.