Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to define another preprocessor directive?

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:

  1. error C2121: '#' : invalid character : possibly the result of a macro expansion
  2. error C2146: syntax error : missing ';' before identifier 'VALUE'
  3. error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  4. error C2144: syntax error : 'void' should be preceded by ';'
  5. 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 #.

like image 863
ST3 Avatar asked Aug 27 '13 20:08

ST3


People also ask

How many preprocessor directives are there?

There are 4 Main Types of Preprocessor Directives: Macros. File Inclusion. Conditional Compilation. Other directives.

What are the different preprocessor directives?

Preprocessor directives consist of the following: Macro definition directives, which replace tokens in the current file with specified replacement tokens. File inclusion directives, which imbed files within the current file. Conditional compilation directives, which conditionally compile sections of the current file.

Why are preprocessor required Explain any two preprocessor directives?

Preprocessor directives, such as #define and #ifdef , are typically used to make source programs easy to change and easy to compile in different execution environments. Directives in the source file tell the preprocessor to take specific actions.

What are preprocessor directives list any two preprocessor directives?

To execute a preprocessor program on a certain statement, some of the preprocessor directives types are: #define: It substitutes a preprocessor using macro. #include: It helps to insert a certain header from another file. #undef: It undefines a certain preprocessor macro.


3 Answers

C 2011 (N1570) 6.10.3.4 3: “The resulting completely macro-replaced preprocessing token sequence is not processed as a preprocessing directive even if it resembles one,…”

C++ 2010 (N3092) 16.3.4 [cpp.rescan] 3 has exactly the same text.

like image 171
Eric Postpischil Avatar answered Sep 17 '22 05:09

Eric Postpischil


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.)

like image 33
AnT Avatar answered Sep 21 '22 05:09

AnT


It does look like your preprocessor is making the substitution you want, but you likely wouldn't get the behaviour you want - the preprocessor is normally just a single pass operation. Example (with clang, but you should be able to reproduce by using the appropriate VS2008 flags):

$ cat example.c 
#define D #define
D VALUE
$ cc -P -E example.c 

 #define VALUE

That #define VALUE is going straight through to the compiler, which won't know what to do with it - it's a preprocessor directive, after all. Clang's error, for reference, is similar to yours:

$ cc -c example.c 
example.c:2:1: error: expected identifier or '('
D VALUE
^
example.c:1:11: note: expanded from macro 'D'
#define D #define
          ^
1 error generated.
like image 23
Carl Norum Avatar answered Sep 20 '22 05:09

Carl Norum