4

I was looking some code, and saw that it used a \ to separate lines while calling a function, does this mean something? Or is it just to be more readable?

        function(\
        lets_say_this_a_long_attribute, \
        and_this_is_another_attribute_with_a_long_name_or_operations, \
        attribute);
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Kareit
  • 77
  • 6
  • 4
    Line continuation. Is it part of a `#define` macro? – Fred Larson Apr 15 '20 at 16:55
  • 7
    Some people are even using it outside of `#define`, totally missing the point.. – Eugene Sh. Apr 15 '20 at 16:56
  • 1
    [This](https://stackoverflow.com/a/6281389/7852589) may be of your interest – If_You_Say_So Apr 15 '20 at 16:58
  • @FredLarson It's not inside a define macro, it's just used in a normal function. – Kareit Apr 15 '20 at 16:59
  • 1
    @RealReza Thank you! I hadn't seen that question and it answers my question. – Kareit Apr 15 '20 at 17:04
  • 2
    See C11 [§5.11.1.2 Translation phases](http://port70.net/~nsz/c/c11/n1570.html#5.1.1.2) and phase 2. It simply means that the following newline and the backslash are removed before the preprocessor gets going on the code. It's pointless; you sometimes find it in code by tyros or newbies, less often in code by experienced programmers. See also GCC [Slightly looser rules on escaping newlines](https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/Escaped-Newlines.html#Escaped-Newlines). – Jonathan Leffler Apr 15 '20 at 17:08

1 Answers1

9

From the C Standard (5.1.1.2 Translation phases)

  1. 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. A source file that is not empty shall end in a new-line character, which shall not be immediately preceded by a backslash character before any such splicing takes place.

For example these physical lines

i\
n\
t\
 x;

form the logical line

int x;

Here is a demonstrative program.

#include <stdio.h>

int main(void) 
{
    i\
n\
t\
 x = 10;

    p\
r\
i\
n\
t\
f
    ( "%d\n", 
    x );

    return 0;
}

Its output is

10

This technique is used for writing macros like for example #define.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 2
    And it happens so early in the processing that you can split comment start and end symbols with backslash-newline pairs. – Jonathan Leffler Apr 15 '20 at 17:10
  • 4
    @machine_1: code obfuscators routinely do it, chopping lines at N characters long, and simply putting a backslash at the end of one line and continuing onto the next. But you're right — sane programmers with no devious intent do not do this. Note that trigraphs are processed before Phase 2; they cannot be split across lines (but `??/` might appear at the end of a line to indicate a backslash-newline pair). – Jonathan Leffler Apr 15 '20 at 17:13
  • 3
    Good answer. I'm just missing a single point: The fact that any `#define` is terminated by the first logical newline character following it, which makes writing of multiline macros impossible without joining several physical lines into a single logical line using the `\-` sequence. And that is also the only valid use in programs that are meant for human consumption. – cmaster - reinstate monica Apr 15 '20 at 17:48
  • 1
    @cmaster-reinstatemonica It can be useful to break complicated `#if` expressions over multiple lines as well. – zwol Apr 15 '20 at 19:00