Why do we need Termination Character In C

Viewed 440

I am new to the C programming language and leaning it recent days. I am little confused on how termination character works and why do we need it. When I look at some documentation on the web, they usually state when we initialize the char array the last character must be '\0'. However when I don't insert it to the end of char array it seems also compile and works well. For example:

char test[4] = "test";
printf("%s\n", test );
printf("%lu\n", strlen(test) );

it compiles, print out the value properly and also strlen return the correct value which means the compiler knows where is the last character in the char array. Then why do we have to add '\0' at the end. Is this JUST the convention? Or there is something else?

Thanks.

2 Answers

You explictely declared test as an array of four chars, therefore it is an array of four chars, containing 't', 'e', 's', and 't'.

printf ("%s") expects a zero-terminated string, and so does strlen. You have an array of four characters, not containing a zero, so passing this to printf or strlen is undefined behaviour.

Since your array is followed by unknown bytes, it may be by pure coincidence that the next byte is a zero. And if that is the case, then it may be that "test" is printed and strlen() returns 4. But that is pure coincidence.

What can also happen is that your program crashes. Or that it prints "testgarbagegarbagegarbage" and strlen returns some large number. Or that your program happily works as expected while you are developing it, and that it crashes when the first paying customer is using it.

Your code has undefined behaviour, which just means anything can happen. You fix this by declaring char test[] = "test"; which will make test[] large enough to hold four chars and a zero byte.

The compiler knows the length of statically-declared arrays. But C is more flexible than that, it allows you to use more general pointers. When you're accessing an array through a pointer, there's no way for the compiler to know which array it points to. In particular, when the parameter of a function is a string, it could be any string in the program.

Consider the following code:

char test1[4] = "test";
char test2[8] = "12345678";
char *test = (rand() % 2 == 0) ? test1 : test2;
printf("%s\n", test);

There's no way for the compiler to know the length of test -- depending on the random choice it could be 4 or 8.

You also can't simply store the length at the beginning of the string, because C allows you to make pointers to any array element. Consider:

char test1[8] = "12345678";
char *test = &test[rand() % 8];
printf("%s\n", test);

There are other languages that solve this problem using indirection. For instance, C++ has the std::string class, which uses a structure that contains the length and a pointer to the string contents. When you create substrings it allocates a new structure and copies the data. But C is a simpler language, and is designed to allow more direct memory access.

Related