Question

In C, a preprocessor statement can be extended over multiple lines with a \ character.

#define SWAP(a, b)  {                   \
                        a ^= b;         \
                        b ^= a;         \ 
                        a ^= b;         \
                    }

This will spread the macro out over several lines, but is there a way to cut a macro off before the end of a line? ie: I wanted to write code on the same line as a macro, but did not want it to be part of the macro. For example, is there a way to make the following possible?

#include <stdio.h> (some character here to end macro) int main(int argc, const char **argv)  { return 0; }

Is there a way to legally fit the preprocessor statement and the main function (or any other code) on the same line?

Was it helpful?

Solution

No, not for the #include statements you're specifically mentioning in your question. And, though I haven't specifically checked(a), that's probably the case for all preprocessing directives.

The standard states that the newline is an integral part of the syntax (from C11 6.10.2 Source file inclusion):

A preprocessing directive of the form # include <h-char-sequence> new-line ...

You can place a comment after an #include directive since replacements of comments with a single space happens in phase 3, whereas pre-processing directives are handled in phase 4 (see C11 5.1.1.2 Translation phases).

But that doesn't help you with trying to place non-comments like the main() function definition on the same line, that's still a no-no.

But my question to you would be: why on Earth would you want to do this? Simply output two lines and be done with it. It's not like we're suffering a world-wide shortage of newline characters :-)


(a) Jonathan Leffler has checked, so this statement is almost certainly correct.

From ISO/IEC 9899:2011, §6.10 Preprocessing directives:

# if constant-expression new-line groupopt
# ifdef identifier new-line groupopt
# ifndef identifier new-line groupopt
# elif constant-expression new-line groupopt
# else new-line groupopt
# endif new-line
# include pp-tokens new-line
# define identifier replacement-list new-line
# define identifier lparen identifier-listopt ) replacement-list new-line
# define identifier lparen ... ) replacement-list new-line
# define identifier lparen identifier-list , ... ) replacement-list new-line
# undef identifier new-line
# line pp-tokens new-line
# error pp-tokensopt new-line
# pragma pp-tokensopt new-line
# new-line

OTHER TIPS

First non white character of a line for a macro must be # and macro extends until the end of line(after line concatenation \ characters). So you can not "Cut off Preprocessor Line".

If you want to do this for some crazy trick answer, you can better use -D or /D (which ever applicable) during compilation and avoid macro completely.

A sane programming requirement would never require any such feature.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top