What does the backslash do in this case?

Hello, I'm sorry for using a vague title. I've been learning about OpenGL shaders and came across this example:

1
2
3
4
5
6
7
8
9
10
11
const GLchar *vertex_shader_code =
{
	"#version 150\n"\

	"in vec2 position;\n"\

	"void main()\n"\
	"{\n"\
	"	gl_Position = vec4(position, 0.0, 1.0);\n"\
	"}\n"
};


What do the backslashes after each string do? My guess is that they allow us to break down a long string so that it can be written over multiple lines.

Thank you!
Last edited on
The backslashes after each string literal serve no purpose, the code would have the same meaning without them. I have no idea why they are there.
Last edited on
Thanks! I tried to replace the slashes with commas but that gave a error which confused me.

1
2
3
4
5
char *identifier = { "a" "b" "c" };

cout << *identifier;
cout << *(identifier + 1);
cout << identifier[2];


I believe { "a" "b" "c" } returns the pointer to an array and that pointer is stored in identifier. Why does char *identifier = { "a", "b", "c" } or even int *int_array = { 1, 2, 3 } not work then when int int_array[] = { 1, 2, 3 } is fine?
Last edited on
closed account (3hM2Nwbp)
'\' acts as a line continuation token.

1
2
3
4
5
int main()
{
    // this is a \
    comment
}


is a valid C++ program, obviously written by someone that loves home brewed source code obfuscation.

The code you've posted is equivalent to:
1
2
3
4
5
6
const GLchar *vertex_shader_code =
{
	"#version 150\n"
	"in vec2 position;\n"
	"void main()\n"	"{\n"	"	gl_Position = vec4(position, 0.0, 1.0);\n"	"}\n"
};

after the continuations are parsed.
Last edited on
Same way this compiles:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#\
i\
n\
c\
l\
u\
d\
e\
<\
i\
o\
s\
t\
r\
e\
a\
m\
>

i\
n\
t \
m\
a\
i\
n\
(
)
{
s\
t\
d\
:\
:
c\
o\
u\
t
<\
<
"Hello, world!"
<\
<
s\
t\
d\
:\
:
e\
n\
d\
l\
;
}
Hello, world!


http://ideone.com/aL2RZB
I think the missing piece is not what the backslash is for, but rather how in C and C++ string literals can be concatenated. The two lines below are identical from the compiler's perspective:
1
2
char const *a = "He" "ll" "o " "wo" "rl" "d!";
char const *b = "Hello world!";
That cleared everything up, thank you all for your help :)
Topic archived. No new replies allowed.