1

For example, I define AA for three times, is it legal?:

#include<stdio.h>
#define AA 10
#define AA 20
#define AA 30
int main() {
    printf("AA");
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
peng xu
  • 385
  • 1
  • 3
  • 6

4 Answers4

10

This is not legal in both C and C++.

Quotes from draft C standard N1570:

6.10.3 Macro replacement

Constraints

1 Tw o replacement lists are identical if and only if the preprocessing tokens in both have the same number, ordering, spelling, and white-space separation, where all white-space separations are considered identical.

2 An identifier currently defined as an object-like macro shall not be redefined by another #define preprocessing directive unless the second definition is an object-like macro definition and the two replacement lists are identical. Likewise, an identifier currently defined as a function-like macro shall not be redefined by another #define preprocessing directive unless the second definition is a function-like macro definition that has the same number and spelling of parameters, and the two replacement lists are identical.

Quotes from draft C++ standard N4582:

16.3 Macro replacement [cpp.replace]

1 Two replacement lists are identical if and only if the preprocessing tokens in both have the same number, ordering, spelling, and white-space separation, where all white-space separations are considered identical.

2 An identifier currently defined as an object-like macro may be redefined by another #define preprocessing directive provided that the second definition is an object-like macro definition and the two replacement lists are identical, otherwise the program is ill-formed. Likewise, an identifier currently defined as a function-like macro may be redefined by another #define preprocessing directive provided that the second definition is a function-like macro definition that has the same number and spelling of parameters, and the two replacement lists are identical, otherwise the program is ill-formed.

cpplearner
  • 13,776
  • 2
  • 47
  • 72
6

No it is not. The C standard clearly states that it is a constraint violation, 6.10.3 p.2:

An identifier currently defined as an object-like macro shall not be redefined by another #define preprocessing directive unless the second definition is an object-like macro definition and the two replacement lists are identical.

So as for all constraint violations, your compiler is only obliged to issue a "diagnostic" that is an explanatory message. It may or may not continue to compile your code.

To state it more directly, your code erroneous and your compiler must tell you.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
5

It is obviously not recommended even thought it can compile. In your example, you can just use #define AA 30 once. In other cases, if you want to define a macro when it is not defined yet, you can use conditionals:

#ifndef AA
#define AA 30
#endif

Also, I think you mean printf("%d\n", AA); to print the macro, because printf("AA"); will just print the string literal AA.

fluter
  • 13,238
  • 8
  • 62
  • 100
  • @LPs This is debatable, the compile warns for the redefinition, but the expanded is 30. – fluter May 02 '16 at 07:02
  • 1
    Yes. Exacly what I wrote. OP code gives 30, your gives something different . – LPs May 02 '16 at 07:05
  • I don't get it? what do you mean? – fluter May 02 '16 at 07:06
  • Why printf("AA"); will only print the string literal "AA", but not the macro's value "30" – peng xu May 02 '16 at 07:07
  • Nothing important at all. I mean that your solution leave the first definition value to AA, but It avoid warnings at all. OP code generates warns and the value will be 30. – LPs May 02 '16 at 07:07
  • 1
    @pengxu Because the `"AA"` is a string literal, the thing inside the double quotes will not be treated as a macro. – fluter May 02 '16 at 07:08
3

Lets start by correcting your printf to something useful: printf("%d", AA);

Compiling it with gcc will produce two warnings that "AA" is redefined. Warnings are really important and should be avoided in C, but the result will be as expected (30).

Julie Pelletier
  • 1,740
  • 1
  • 10
  • 18