Reputation: 991
the following shader is said to be one string and a null-terminated string.
A shader:
const GLchar* VertexShader =
{
"#version 330\n"\
"layout(location=0) in vec4 in_Position;\n"\
"layout(location=1) in vec4 in_Color;\n"\
"out vec4 ex_Color;\n"\
"void main(void)\n"\
"{\n"\
" gl_Position = in_Position;\n"\
" ex_Color = in_Color;\n"\
"}\n"
};
My questions are:
Upvotes: 1
Views: 335
Reputation: 81409
What are the slashes mean at the end of each line? Moreover, why the last line doesn't has a slash?
It is a line continuation, it means the current line continues in the next one.
There are several strings in the shader, why the shader is said to have only one string?
Sequential string literals are collapsed into a single one. So "ab" "c"
becomes "abc"
.
Why it is said to be a null-terminated string? (Since there is no '\0')
String literals are null-terminated. So "ab"
is actually {'a', 'b', '\0'}
. Note that when string literals are collapsed, all but the last implicit null-termination characters are removed.
Upvotes: 6
Reputation: 110768
Whenever there is a backslash (\
) followed by a new line, those characters are deleted, splicing the two lines together. This occurs early on in the phases of translation, even before preprocessing directives are executed. This gives you the difference between a physical source line and a logical source line. Logically, the code is equivalent to the following:
const GLchar* VertexShader =
{
"#version 330\n" "layout(location=0) in vec4 in_Position;\n" "layout(location=1) in vec4 in_Color;\n" "out vec4 ex_Color;\n"
"void main(void)\n" "{\n" " gl_Position = in_Position;\n" " ex_Color = in_Color;\n" "}\n"
};
However, this line splicing is completely unnecessary here.
Later on in the phases of translation (after preprocessing directives are executed), any adjacent string literal tokens are concatenated. That is they are joined as though they were just a single string literal. All of the string literals in the above code will be concatenated (even where the new line is separating them).
A string literal always gives you an array of const char
that is one character large than the string literal because it adds an \0
character to the end. That is, a string literal always gives you a null-terminated string.
Upvotes: 4
Reputation: 122011
What are the slashes mean at the end of each line? Moreover, why the last line doesn't has a slash?
It is a line continuation, and is superfluous here.
There are several strings in the shader, why the shader is said to have only one string?
From the C99 standard section on String Literals:
In translation phase 6, the multibyte character sequences specified by any sequence of adjacent character and wide string literal tokens are concatenated into a single multibyte character sequence. If any of the tokens are wide string literal tokens, the resulting multibyte character sequence is treated as a wide string literal; otherwise, it is treated as a character string literal.
The string literals in posted code are concatenated into a single string literal.
Why it is said to be a null-terminated string? (Since there is no '\0')
From the same section of C99 standard:
In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals
A null character is added to each string literal (after concatenation).
Upvotes: 2
Reputation: 145899
The backslash eat the immediate following end of line character.
Example
"hello" \
" world"
is the same as
"hello" " world"
which is by the way the same as
"hello world"
Upvotes: 1
Reputation: 60848
Upvotes: 1
Reputation: 258668
The slashes are line continuation symbols. It just means the lines are concatenated in a single line. It's written like that for readability. Imagine instead you had
"layout(location=0) in vec4 in_Position;\n" "layout(location=1) in vec4 in_Color;\n" "out vec4 ex_Color;\n"\
There is only one string because string literals are automatically concatenated when written one after the other:
"abc" "xyz"
is equivalent to the string "abcxyz"
.
Upvotes: 1