Reputation: 15
I was wondering if there was a way in C language to define #define
like this:
#define something #define
something a 42
something b 42
Upvotes: 1
Views: 796
Reputation: 753725
No, there isn't. If the expansion of a macro generates something that looks like a preprocessor directive, it is not processed as one, leaving a #
in the source code that is seen by the compiler proper, which will then object that the #
is an unexpected token (syntax error).
6.10.3.4 Rescanning and further replacement
¶3 The resulting completely macro-replaced preprocessing token sequence is not processed as a preprocessing directive even if it resembles one, but all pragma unary operator expressions within it are then processed as specified in 6.10.9 below.
The 'pragma unary operator' referred to is the _Pragma()
operator which takes a string literal.
The wording in C99 is very similar, and the wording is C89 is similar but doesn't mention the _Pragma
operator because it didn't exist in C89.
You can find drafts of the C2011 standard at the Open Standard web site:
along with working papers, 'mailings' for the committee meetings, etc.
(JTC1 is Joint Technical Committee 1; SC22 is Standardization Committee 22 for programming languages; WG14 is Working Group 14, responsible for the C standard. WG21 is responsible for the C++ standard.)
You can obtain your own, personalized copy of the PDF of the standard from ANSI for 30 USD. I regard that as a necessary investment for any serious C programmer.
Upvotes: 2
Reputation: 121387
No, it's not possible in C. Defining a macro in another macro is not allowed.
From C standard:
6.10.3.4 Rescanning and further replacement
3 The resulting completely macro-replaced preprocessing token sequence is not processed as a preprocessing directive even if it resembles one, but all pragma unary operator expressions within it are then processed as specified in 6.10.9 below.
Upvotes: 5
Reputation: 376
If you want to define something based on the definition of other ,C provides #ifdef
to achieve it
like:-
#define something
#ifdef something
#define a 42
#else
#define b 42
#endif
Upvotes: 0
Reputation: 35803
No. The preprocessor only does one pass, so in the end, the code that goes to the compiler includes a #define, which is a syntax error.
Upvotes: 1