In many code samples, people usually use '\0'
after creating a new char array like this:
string s = "JustAString";
char* array = new char[s.size() + 1];
strncpy(array, s.c_str(), s.size());
array[s.size()] = '\0';
Why should we use '\0'
here?
'\0' is referred to as NULL character or NULL terminator It is the character equivalent of integer 0(zero) as it refers to nothing In C language it is generally used to mark an end of a string.
They are null-terminated because whole plenty of Standard Library functions expects them to be. And also because that is how the C language spec says that string literals are encoded.
Here, 'f' represents the character ``f'', etc., and '\0' represents the so-called null character (ASCII code 0), which is used in C to signal the termination of a character string. The null character is automatically added to the end of any character string enclosed in double quotes.
C strings are null-terminated. That is, they are terminated by the null character, NUL . They are not terminated by the null pointer NULL , which is a completely different kind of value with a completely different purpose. NUL is guaranteed to have the integer value zero.
The title of your question references C strings. C++ std::string
objects are handled differently than standard C strings. \0
is important when using C strings, and when I use the term string
here, I'm referring to standard C strings.
\0
acts as a string terminator in C. It is known as the null character, or NUL. It signals code that processes strings - standard libraries but also your own code - where the end of a string is. A good example is strlen
which returns the length of a string.
When you declare a constant string with:
const char *str = "JustAString";
then the \0
is appended automatically for you. In other cases, where you'll be managing a non-constant string as with your array example, you'll sometimes need to deal with it yourself. The docs for strncpy, which is used in your example, are a good illustration: strncpy
copies over the null termination characters except in the case where the specified length is reached before the entire string is copied. Hence you'll often see strncpy
combined with the possibly redundant assignment of a null terminator. strlcpy
and strcpy_s
were designed to address the potential problems that arise from neglecting to handle this case.
In your particular example, array[s.size()] = '\0';
is one such redundancy: since array
is of size s.size() + 1
, and strncpy
is copying s.size()
characters, the function will append the \0
.
The documentation for standard C string utilities will indicate when you'll need to be careful to include such a null terminator. But read the documentation carefully: as with strncpy
the details are easily overlooked, leading to potential buffer overflows.
Why are strings in C++ usually terminated with
'\0'
?
Note that C++ Strings and C strings are not the same.
In C++ string refers to std::string which is a template class and provides a lot of intuitive functions to handle the string.
Note that C++ std::string are not \0
terminated, but the class provides functions to fetch the underlying string data as \0
terminated c-style string.
In C a string is collection of characters. This collection usually ends with a \0
.
Unless a special character like \0
is used there would be no way of knowing when a string ends.
It is also aptly known as the string null terminator.
Ofcourse, there could be other ways of bookkeeping to track the length of the string, but using a special character has two straight advantages:
Note that \0
is needed because most of Standard C library functions operate on strings assuming they are \0
terminated.
For example:
While using printf()
if you have an string which is not \0
terminated then printf()
keeps writing characters to stdout
until a \0
is encountered, in short it might even print garbage.
Why should we use
'\0'
here?
There are two scenarios when you do not need to \0
terminate a string:
\0
to strings. In your case you already have the second scenario working for you.
array[s.size()] = '\0';
The above code statement is redundant in your example.
For your example using strncpy()
makes it useless. strncpy()
copies s.size()
characters to your array
, Note that it appends a null termination if there is any space left after copying the strings. Since array
is of size s.size() + 1
a \0
is automagically added.
'\0' is the null termination character. If your character array didn't have it and you tried to do a strcpy you would have a buffer overflow. Many functions rely on it to know when they need to stop reading or writing memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With