Consider the following program:
#define ALPHABET_SIZE 'z' - 'a' + 1
#include <stdio.h>
int main(void)
{
printf("%d\n", 2*ALPHABET_SIZE);
printf("%d\n", ALPHABET_SIZE*2);
}
In my C implementation, this prints “148” and “27”. This occurs because, in the first printf
, 2*ALPHABET_SIZE
is replaced with 2*'z' - 'a' + 1
, which is evaluated as (2*'z') - 'a' + 1
, and, in the second printf
, ALPHABET_SIZE*2
is replaced with 'z' - 'a' + 1*2
, which is evaluated as 'z' - 'a' + (1*2)
. Since those produce two different results, it proves that preprocessing with C semantics does not replace a macro with a single expression result computed once; it must produce something else (actually a sequence of preprocessor tokens) that is subsequently reinterpreted in context.