peng xu
peng xu

Reputation: 415

#define many times without #undef,is it legal?

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");
}

Upvotes: 1

Views: 700

Answers (4)

cpplearner
cpplearner

Reputation: 15813

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.

Upvotes: 10

Jens Gustedt
Jens Gustedt

Reputation: 78903

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.

Upvotes: 6

Julie Pelletier
Julie Pelletier

Reputation: 1716

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).

Upvotes: 3

fluter
fluter

Reputation: 13786

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.

Upvotes: 5

Related Questions