For example, I define AA for three times, is it legal?:
#include
#define AA 10
#define AA 20
#define AA 30
int main() {
printf(\"
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.
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).
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.
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
.