28

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]='\0';
cout << x << endl;
cout<<"hhh\0defef\n"<<endl;

Result:

hhhdef

hhhef

hhh

Can anyone give me some pointers?

1
  • Where do you find this snippet?
    – Cloud Cho
    Commented Jan 22 at 19:24

6 Answers 6

38

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
13

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
11

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

5

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

4

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
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.