What does '\0' mean?

后端 未结 6 1689
梦如初夏
梦如初夏 2020-12-29 05:55

I can\'t understand what the \'\\0\' in the two different place mean in the following code:

string x = \"hhhdef\\n\";
cout << x << endl;
x[3]=\'\         


        
相关标签:
6条回答
  • 2020-12-29 06:23

    C++ has two string types:

    The built-in C-style null-terminated strings which are really just byte arrays and the C++ standard library std::string class which is not null terminated.

    Printing a null-terminated string prints everything up until the first null character. Printing a std::string prints the whole string, regardless of null characters in its middle.

    0 讨论(0)
  • 2020-12-29 06:27

    C++ std::strings are "counted" strings - i.e., their length is stored as an integer, and they can contain any character. When you replace the third character with a \0 nothing special happens - it's printed as if it was any other character (in particular, your console simply ignores it).

    In the last line, instead, you are printing a C string, whose end is determined by the first \0 that is found. In such a case, cout goes on printing characters until it finds a \0, which, in your case, is after the third h.

    0 讨论(0)
  • 2020-12-29 06:29

    The \0 is treated as NULL Character. It is used to mark the end of the string in C.

    In C, string is a pointer pointing to array of characters with \0 at the end. So following will be valid representation of strings in C.

    char *c =”Hello”;    // it is actually Hello\0
    char c[] = {‘Y’,’o’,’\0′};
    

    The applications of ‘\0’ lies in determining the end of string .For eg : finding the length of string.

    0 讨论(0)
  • 2020-12-29 06:32

    You're representing strings in two different ways here, which is why the behaviour differs.

    The second one is easier to explain; it's a C-style raw char array. In a C-style string, '\0' denotes the null terminator; it's used to mark the end of the string. So any functions that process/display strings will stop as soon as they hit it (which is why your last string is truncated).

    The first example is creating a fully-formed C++ std::string object. These don't assign any special meaning to '\0' (they don't have null terminators).

    0 讨论(0)
  • 2020-12-29 06:35

    The \0 is basically a null terminator which is used in C to terminate the end of string character , in simple words its value is null in characters basically gives the compiler indication that this is the end of the String Character Let me give you example - As we write printf("Hello World"); /* Hello World\0 here we can clearly see \0 is acting as null ,tough printinting the String in comments would give the same output .

    0 讨论(0)
  • 2020-12-29 06:41

    \0 is the NULL character, you can find it in your ASCII table, it has the value 0.

    It is used to determinate the end of C-style strings.

    However, C++ class std::string stores its size as an integer, and thus does not rely on it.

    0 讨论(0)
提交回复
热议问题