1

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?

Yktula
  • 14,179
  • 14
  • 48
  • 71
  • Dupe http://stackoverflow.com/questions/98944/how-to-generate-a-newline-in-a-cpp-macro –  Apr 25 '10 at 18:22
  • 3
    You can't generate a `#include` with macros. – kennytm Apr 25 '10 at 18:24
  • 2
    @Neil Butterworth: No dupe; that other question is about macros expanding to newline characters; this one is about writing C oneliners (or something), without a macro in sight. – Thomas Apr 25 '10 at 19:20

4 Answers4

1

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
  • 224,562
  • 31
  • 268
  • 324
1

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
  • 174,939
  • 50
  • 355
  • 478
1

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
  • 134,909
  • 25
  • 265
  • 421
0

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
  • 134,909
  • 25
  • 265
  • 421