Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple preprocessor directives on one line in C++

A hypothetical question: Is it possible to have a C++ program, which includes preprocessor directives, entirely on one line?

Such a line would look like this:

#define foo #ifdef foo #define bar #endif

What are the semantics of such a line?

Further, are there any combinations of directives which are impossible to construct on one line?

If this is compiler-specific then both VC++ and GCC answers are welcome.

like image 545
davetapley Avatar asked Aug 20 '10 15:08

davetapley


People also ask

How many preprocessors are there in C?

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

What is the difference between #ifdef and #ifndef in C?

Use the #ifdef statement when you want to compile a section only if a specified expression has been defined with #define. Use #ifndef when you want to compile a section only if a specified expression has not been defined.

What is #if #endif in C?

In the C Programming Language, the #endif directive is used to define the following directives: #if, #ifdef , and #ifndef . Whenever the #endif directive is encountered in a program, it determines if the preprocessing of #if, #ifdef, or #ifndef has been completed successfully.


1 Answers

A preprocessing directive must be terminated by a newline, so this is actually a single preprocessing directive that defines an object-like macro, named foo, that expands to the following token sequence:

# ifdef foo # define bar # endif

Any later use of the name foo in the source (until it is #undefed) will expand to this, but after the macro is expanded, the resulting tokens are not evaluated as a preprocessing directive.

This is not compiler-specific; this behavior is defined by the C and C++ standards.

like image 132
James McNellis Avatar answered Sep 17 '22 17:09

James McNellis