tags:

views:

82

answers:

4

Is there a character sequence recognized as a newline that's defined by the C standard and/or recognized by GCC? How can newlines be simulated after preprocessor directives to have them and C code share the same line? How about using digraphs or trigraphs?

#include <stdlib.h> [NEWLINE] int main() { exit(EXIT_SUCCESS); }

http://en.wikipedia.org/wiki/C_preprocessor#Multiple_lines says that they end at the first line which does not end in a backslash. How can we have them end within a line?

+1  A: 

Unfortunately, this is not possible with macros.

You could, however, create a .h file to have the same effect; eg, have a myheader.h:

#include <stdlib.h>
int main() { exit(EXIT_SUCCESS); }

And then in your other files:

#include "myheader.h"
bdonlan
It's only unfortunate if there is some actual use case that gets harder because of it. And I don't count code-golf.
dmckee
A: 

Depending whom you are trying to fool, a vertical-tab character (control-K) might do the trick.

Or, it might not. I looked again at the standard and apparently vertical tabs don't count as newlines.

And a comp.std.c++ post today tells me that vertical tab in a preprocessing line is simply illegal (§16/2).

Potatoswatter
+1  A: 

This is not possible, at least not on GCC. From the GCC documentation:

Except for expansion of predefined macros, all these operations are triggered with preprocessing directives. Preprocessing directives are lines in your program that start with `#'.

So the preprocessor must read an end-of-line after the include directive for this to work:

Different systems use different conventions to indicate the end of a line. GCC accepts the ASCII control sequences LF, CR LF and CR as end-of-line markers.

So you cannot use a newline sequence from a different platform, at least not on GCC.

There is no digraph for the newline:

Digraph:        <%  %>  <:  :>  %:  %:%:
Punctuator:      {   }   [   ]   #    ##

Nor is there a trigraph:

Trigraph:       ??(  ??)  ??<  ??>  ??=  ??/  ??'  ??!  ??-
Replacement:      [    ]    {    }    #    \    ^    |    ~
Thomas
+1  A: 

Depending what you are trying to accomplish, the #line directive might do the trick.

#include <stdlib.h>
#line 1
int main() { exit(EXIT_SUCCESS); } /* this is on line 1 */

or more generally

#line __LINE__ - 1
Potatoswatter