Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#define multiple constants on one line in C

Is it possible to put several #define preprocessor commands together in C?

For example, instead of this:

#define a 1
#define b 2
#define c 3 ...

just this:

#define a1, b2, c3 
like image 341
Pyrons Avatar asked Dec 25 '22 03:12

Pyrons


2 Answers

No, the preprocessor phase is just text replacement and meant to be simple to parse. Therefore all preprocessor directives

  • start on a line (after eventual white space) with #
  • end with the end of the line
  • do only one thing at a time

in particular a macro definition via #define extends until the end of the line and the preprocessing phase wouldn't be able to decide that you intend to declare multiple macros in one go. In the syntax that you propose the content of the macro a would in fact be , b2, c3 which is probably not what you want :)

like image 124
Jens Gustedt Avatar answered Dec 27 '22 17:12

Jens Gustedt


No. According to the latest C99 standard, http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf page 146, as well as the current C11 standard, http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1570.pdf page 161, the syntax of a control-line beginning with #define has only one identifier (not counting function-like arguments in an identifier-list) and one replacement-list before the new-line character.

like image 39
Chris Culter Avatar answered Dec 27 '22 18:12

Chris Culter