I've been looking through code golf and got an idea to try this code:
#define D #define
after adding this line, everything worked fine, however I expanded it into this:
#define D #define
D VALUE
And here I got 5 compilation error. If I change D
into #define
everything is fine, can someone explain, why this code is illegal?
NOTE: I used VS2008 compiler.
EDIT: After some answers I see that I needed to give compilations error list:
- error C2121: '#' : invalid character : possibly the result of a macro expansion
- error C2146: syntax error : missing ';' before identifier 'VALUE'
- error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
- error C2144: syntax error : 'void' should be preceded by ';'
- error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
First error shows that D
is not just define
but also includes #
.
This code is illegal because language specification says it is illegal. According to C and C++ preprocessor specification, whatever code you build using preprocessor will never be interpreted as another preprocessor directive. In short, you cannot build preprocessor directives using preprocessor. Period.
(Also, you cannot build comments using preprocessor.)