简体   繁体   中英

About the null-terminated strings (confused)

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:

  1. What are the slashes mean at the end of each line? Moreover, why the last line doesn't has a slash?
  2. There are several strings in the shader, why the shader is said to have only one string?
  3. Why it is said to be a null-terminated string? (Since there is no '\\0')

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.

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.

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).

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" .

  1. Means to escape the following character, which looks to be a newline in this case; I think this is unnecessary/extraneous here.
  2. It is just one string - call printf(VertexShader[0]) on it to try.
  3. When you declare a C string a \\0 is automatically appended. You do not specify this manually, in fact if you did you would get two \\0 I believe.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM