Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whitespace character after backslash in C and C++

Tags:

c++

c

What C and C++ standards says about whitespace character (or several characters) after backslash? Does it guarantees to join lines anyway or not?

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

MSVC and gcc works different in this case.

like image 403
FrozenHeart Avatar asked Dec 15 '22 19:12

FrozenHeart


1 Answers

For reference, the standard quote is (§2.2/1, abridged, emphasis mine):

Phases of Translation

[...]
2. Each instance of a backslash character (\) immediately followed by a new-line character is deleted, splicing physical source lines to form logical source lines. Only the last backslash on any physical source line shall be eligible for being part of such a splice. If, as a result, a character sequence that matches the syntax of a universal-character-name is produced, the behavior is undefined. A source file that is not empty and that does not end in a new-line character, or that ends in a new-line character immediately preceded by a backslash character before any such splicing takes place, shall be processed as if an additional new-line character were appended to the file.
[...]

The implementation-defined part that other answers are mentioning is in the definition of "new-line".

(Note that comments are not replaced until phase 3, so that 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 appended to the end of the comment, then ultimately removed.)

like image 131
3 revs Avatar answered Dec 26 '22 11:12

3 revs