5
#define A;

#ifdef A
    (...)
#endif

I thought the predecessor would take this as false; however it would goes into this condition. Is A and A; taken as the same?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Renjie
  • 51
  • 1
  • 4

1 Answers1

13

No, they're distinct.

In

#define A;

A and ; are two distinct tokens. A is the macro name, and ; is its definition. So you could, if you really wanted to, write:

printf("Hello, world\n")A

and it would be equivalent to

printf("Hello, world\n");

(But please don't do that.)

Since the only thing you do with A is refer to it in an #ifdef, all you're doing is testing whether it's been defined or not, regardless of how it's defined. The semicolon is irrelevant because you don't use it.

Just as a matter of style and clarity, you should always have a space between a macro name and its definition:

#define A ;

This is particularly important if the first token of the expansion is a ( character. If it immediately follows the macro name, you have a function-like macro definition (the macro takes arguments). If there's a space between the macro name and the (, the ( is just part of what the macro expands to.

Speaking of semicolons, a common error is including unnecessary semicolons in macro definitions:

#define THE_ANSWER 42;

...

printf("The answer is %d\n", THE_ANSWER);

Since the semicolon is part of the macro definition, this expands to:

printf("The answer is %d\n", 42;);

which is a syntax error.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631