What does the \ 0 character in string C mean?

Consider the following code:

char str[]= "Hello\0"; 

What is the length of the str array and how many windows does it end with?

+46
c ++ c string
Jan 17 '11 at 9:02
source share
5 answers

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

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

The key point here is that the implicit terminator nul is always added, even if the string literal ends 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 size of the array, the string will be truncated to match:

 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, however, is rarely useful and prone to calculate the length of the string and end with an inexhaustible string. This is also forbidden in C ++.

+79
Jan 17 '11 at 9:05
source share
β€” -

The length of the array is 7, the character NUL \0 is still considered a character, and the string still ends with implicit \0

See this link to see a working example.

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

& section; 6.7.8 / p14
An array of character type can be initialized to a character string literal, optionally enclosed in curly braces. Sucessive character string characters literal (including the terminating null character , if there is space , 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 */ 
+10
Jan 17 2018-11-11T00: 00Z
source share

Breaking up your regular JUST TRY IT drum solo, here's how you can answer these questions in the future:

 $ cat junk.c #include <stdio.h> char* string = "Hello\0"; int main(int argv, char** argc) { printf("-->%s<--\n", string); } $ gcc -S junk.c $ cat junk.s 

... eliminating unnecessary parts ...

 .LC0: .string "Hello" .string "" 

...

 .LC1: .string "-->%s<--\n" 

...

Notice how the line I used for printf is just "-->%s<---\n" , and the global line consists of two parts: "Hello" and "" . The GNU assembler also terminates lines with the implicit NUL , so the fact that the first line (.LC0) is in these two parts indicates the presence of two NUL s. So the string is 7 bytes long. Generally, if you really want to know what your compiler does with a specific piece of code, isolate it in a fictitious example like this and see what it does using -S (for GNU - MSVC also has a flag for outputting assembler, but I don’t know what this hand is). You will learn a lot about how your code works (or does not work depending on the circumstances), and you will receive a response quickly, which is 100% guaranteed to correspond to the tools and the environment in which you work.

+5
Jan 17 '11 at 9:21
source share

What is the length of the str array and how many windows does it end with?

Let me know:

 int main() { char str[] = "Hello\0"; int length = sizeof str / sizeof str[0]; // "sizeof array" is the bytes for the whole array (must use a real array, not // a pointer), divide by "sizeof array[0]" (sometimes sizeof *array is used) // to get the number of items in the array printf("array length: %d\n", length); printf("last 3 bytes: %02x %02x %02x\n", str[length - 3], str[length - 2], str[length - 1]); return 0; } 
+3
Jan 17 2018-11-11T00:
source share

In particular, I want to mention one situation with which you can confuse.

What is the difference between "\ 0" and ""?

The answer is that "\0" represents in the array {0 0} and "" is {0} .

Because "\0" is still a string literal, and it will also add "\0" to the end. And "" empty, but also adds "\0" .

Understanding this will help you deeply understand "\0" .

+3
Oct 28 '15 at 7:13
source share



All Articles