Whitespace after backslash in C and C ++

What C and C ++ standards speak of a space character (or multiple characters) after a backslash? Does this guarantee anyway or not?

int main() { // Comment \ int foo; } 

MSVC and gcc work in this case.

+4
c ++ c
Sep 06 '12 at 17:21
source share
2 answers

For reference, the standard quotation (ยง2.2 / 1, abridged, highlighted by me):

Translation phases

[...]
2. Each instance of the backslash character ( \ ) immediately followed by a newline character is deleted by splicing the physical source lines to form the logical lines of the source . Only the last backslash in any physical line of the source should be allowed to be part of such a splicing. If the result is a sequence of characters that matches the syntax of the name of the universal character, the behavior is undefined. A source file that is not empty and that does not end with a newline or ends with a newline that is preceded by a backslash before any such merging occurs is processed as if a new, line symbol is added to the file .
[...]

The implementation-defined part that other answers mention is contained in the definition of a "new line."

(Note that comments are not replaced until step 3, so in this code:

 int main() { int x = 0; // assuming the definition of new-line is as expected, this function // will return 0, not 5 (no whitespace after this backslash: ) \ x = 5; return x; } 

x = 5; will be added to the end of the comment and then deleted.)

+7
Sep 06
source share

In the C standard, its implementation is defined as a text file broken into lines (as part of translation phase 1 if memory is used). For \ -newline purposes, GCC defines a line ending with zero or more horizontal ASCII space characters (SPC, TAB, VT, or FF), followed by one of the three usual ASCII line ending sequences: CR, LF, or CR LF.

I do not know what MSVC does, but I would not be surprised if it is different.

+5
Sep 06
source share



All Articles