sunshilong369
sunshilong369

Reputation: 746

What are the differences bettween null character and '\0'?

The null character is a control character with the value zero.

As per the documentation(http://www.cplusplus.com/reference/string/string/resize/), std::string::resize() could extend current string by value-initialized characters (null characters).

Then, could anybody make me clear about the differences bettween null character and '\0'?

When i use std::string::resize(str) to extend the std::string, it would extend the string with many null characters which may be printed to the terminal when executes std:cout << string << std::endl.You could check it at http://cpp.sh/4lgvl.

It makes me confused as you say that there's a convention that '\0' means "the end of a string".

Upvotes: 0

Views: 114

Answers (1)

NathanOliver
NathanOliver

Reputation: 180490

Then, could anybody make me clear about the differences bettween null character and '\0'?

There isn't any. '\0' is the null character. You can't use '0' as that is the character 0, not a character with the value of 0. To get a character with the value of 0 you need either

char ch = 0;

where you initialize with the integer 0, or you use

char ch = '\0';

where the \0 is an octal escape sequence with the value of 0 so you are initializing from the "null char literal" (made this up, not sure if that is actually its technical name).


There is also

char ch{};
// or
char ch = char();

Where you value initialize ch and for a char, value initialization means zero initialization, so ch has the value of 0 which is null character value.

Upvotes: 6

Related Questions