Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the symbol \0 mean in a string-literal?

Consider following code:

char str[] = "Hello\0"; 

What is the length of str array, and with how much 0s it is ending?

like image 664
UmmaGumma Avatar asked Jan 17 '11 09:01

UmmaGumma


People also ask

What is the mean of \0 in C++?

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.

Why do you need a '\ 0 at the end of a string?

Strings are actually one-dimensional array of characters terminated by a null character '\0'. Thus a null-terminated string contains the characters that comprise the string followed by a null.

Is char * A string literal?

String literals are convertible and assignable to non-const char* or wchar_t* in order to be compatible with C, where string literals are of types char[N] and wchar_t[N]. Such implicit conversion is deprecated.

Does '\ 0 take up memory?

\0 is just one more character from malloc and free perspective, they don't care what data you put in the memory.


2 Answers

sizeof str is 7 - five bytes for the "Hello" text, plus the explicit NUL terminator, plus the implicit NUL terminator.

strlen(str) is 5 - the five "Hello" bytes only.

The key here is that the implicit nul terminator is always added - even if the string literal just happens to end with \0. Of course, strlen just stops at the first \0 - it can't tell the difference.

There is one exception to the implicit NUL terminator rule - if you explicitly specify the array size, the string will be truncated to fit:

char str[6] = "Hello\0"; // strlen(str) = 5, sizeof(str) = 6 (with one NUL) char str[7] = "Hello\0"; // strlen(str) = 5, sizeof(str) = 7 (with two NULs) char str[8] = "Hello\0"; // strlen(str) = 5, sizeof(str) = 8 (with three NULs per C99 6.7.8.21) 

This is, however, rarely useful, and prone to miscalculating the string length and ending up with an unterminated string. It is also forbidden in C++.

like image 174
bdonlan Avatar answered Sep 28 '22 20:09

bdonlan


The length of the array is 7, the NUL character \0 still counts as a character and the string is still terminated with an implicit \0

See this link to see a working example

Note that had you declared str as char str[6]= "Hello\0"; the length would be 6 because the implicit NUL is only added if it can fit (which it can't in this example.)

§ 6.7.8/p14
An array of character type may be initialized by a character string literal, optionally enclosed in braces. Sucessive characters of the character string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.

Examples

char str[] = "Hello\0"; /* sizeof == 7, Explicit + Implicit NUL */ char str[5]= "Hello\0"; /* sizeof == 5, str is "Hello" with no NUL (no longer a C-string, just an array of char). This may trigger compiler warning */ char str[6]= "Hello\0"; /* sizeof == 6, Explicit NUL only */ char str[7]= "Hello\0"; /* sizeof == 7, Explicit + Implicit NUL */ char str[8]= "Hello\0"; /* sizeof == 8, Explicit + two Implicit NUL */ 
like image 37
SiegeX Avatar answered Sep 28 '22 19:09

SiegeX