I cannot find rules for C preprocessor that handle the case below. If you assume a straight substitution this case should not even compile because a=--1
. But if you assume substitution with parenthesis then a=-(-1) = 1
.
#include <stdio.h>
#define ALPHA -1
#define BETA -ALPHA
int main(void) {
int a = BETA;
int b = -ALPHA; // this compiles too, why? b = --1
printf("%d\n",a); // 1
return 0;
}
Preprocessing is the phase which precede compilation it generates a preprocessing translation unit which will be the input for compiler.
Some of those steps in its process are ( From C99 5.1.1.2 ):
The source file is decomposed into preprocessing tokens . (...)
Preprocessing directives are executed, macro invocations are expanded , (...)
As you can see Tokenization precede macro invocation so -ALPHA
will be considered as 2 separate preprocessing tokens (*ie -
and ALPHA
1 ) and not as you thought by 2 preprocessing token --1
( --
and 1
)
After that in rule 7:
- White-space characters separating tokens are no longer significant . Each preprocessing token is converted into a token.
So the compiler will get tokens and ignore whitespaces.
If you generate the preprocessing file with gcc -E
whitespaces have no significance and those you are seeing are only for formatting to user and cannot reflect the real internal behavior of CPP.
From GCC CPP 5.4.0 1.3 :
Tokens do not have to be separated by white space , but it is often necessary to avoid ambiguities (...)
Once the input file is broken into tokens, the token boundaries never change , except when the '##' preprocessing operator is used to paste tokens together
The compiler does not re-tokenize the preprocessor's output . Each preprocessing token becomes one compiler token.
Summarizing:
If you write -ALPHA
=> tokens are:
-
punctuator - binary minus sign
-
punctuator - binary minus sign
1
constant - integer constant
If you write --1
=> tokens are:
--
punctuator - Decrement operator
1
constant - integer constant
Decrement operator should not be used with constant that's why you get error in this case during compilation.
1 : ALPHA
identifier will be replaced (in phase 4) by two preprocessing tokens -
and 1
already identified in macro definition
This is how your program looks after pre-processing is completed.
int main(void) {
int a = - -1;
int b = - -1;
printf("%d\n",a);
return 0;
}
You can view this output using -E
option with gcc
.
It is definitely not --1
. Notice the extra space.
First thing you have to understand that C Preprocessors is not a part of compilation process. It's just the substitution process which tells the compiler to do required pre-processing before the actual compilation.
To understand much you have to read the Macro Pitfalls .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.