views:

62

answers:

1

C++ allows you to use the #define preprocessor directive to define symbolic constants which the compiler will replace before compilation. My question is, how do compilers typically store these internally and do they have data types?

+6  A: 

Strictly speaking, the compiler never sees constants declared with the #define preprocessor directive. These are handled on a textual substitution basis by the preprocessor. They do not have "types" in the C++ sense, since the preprocessor does not know anything about the C++ language semantics.

The preprocessor uses a straightforward text substitution strategy to resolve macros. For example, in the following code:

#define FIVE 5

int a = FIVE;

the compiler will see only:

int a = 5;

The symbol FIVE is gone from the source the compiler sees. Your compiler will have an option to run the preprocessor only; in GCC it is -E and in MSVC it is /E or /P. Using such an option, you can run your source through the preprocessor to see how it is changed.

Greg Hewgill