What happens when a string is cut off by a null character in C?

Viewed 274

For example, I have char s[] = "abcdef" which has a null character at the end. Now, if I do s[3] = '\0', then my string 's' becomes "abc", due to the null character in index three of the array. My question is, what happens to the remaining "def" part of the string? Where does it go? Is it still part of the character array s but when we represent it as a string it cuts off to the point where the null character is found?

Thanks!

3 Answers

The 'd' was overwritten by the '\0', of course.

The "ef" are still there in the array, they didn't go anywhere. Functions that operate on strings will ignore them, since they will stop processing when the null at s[3] is reached. But you can see them if you do something like printf("%c", s[4]) or printf("%s", s+4).

You can find out easily with:

printf('%c',s[4]);

output: e

What is inside your array is still there (except the ascii character d that your replaced from a byte value of 100 'd' to 0 '\0'). The reason why it stops is because by convention Strings are null terminated.

You could perfectly create your own function that read and print input until it is * terminated.

In fact, NULL terminated strings are also referred to as 'C strings' (wikipedia:)

In computer programming, a null-terminated string is a character string stored as an array containing the characters and terminated with a null character (a character with a value of zero, called NUL in this article). Alternative names are C string, which refers to the C programming language and ASCIIZ (although C can use encodings other than ASCII).

Your character array was constructed by the compiler like this:

char s[7] = { 'a', 'b', 'c', 'd', 'e', 'f', '\0' };

The size of s is 7, and it will be fixed. During compile time, your compiler knows this size and you can get it with the operator sizeof.

If you now assign '\0' to s[3], the array looks like this:

char s[7] = { 'a', 'b', 'c', '\0', 'e', 'f', '\0' };

So, the remaining part "def" does not vanish. Instead, 'd' was replaced by '\0', and the following characters are still there.

Since the size of an array is lost after compiling and linking, you need to store it yourself if you want to work with it. Or you need to mark the end of the array with a special value.

This is how the creators of C decided to realize strings. All string function will operate until they find a '\0'.

As an experiment you might like to run this little test:

#include <stdio.h>

int main(void) {
    char s[] = "abcdef";
    size_t i;

    for (i = 0; i < sizeof s; i++) {
        if (s[i] != '\0') {
            printf("Before s[%zu] = '%c' (0x%02X)\n", i, s[i], s[i]);
        } else {
            printf("Before s[%zu] = '\\0' (0x%02X)\n", i, s[i]);
        }
    }

    s[3] = '\0';

    for (i = 0; i < sizeof s; i++) {
        if (s[i] != '\0') {
            printf("After s[%zu] = '%c' (0x%02X)\n", i, s[i], s[i]);
        } else {
            printf("After s[%zu] = '\\0' (0x%02X)\n", i, s[i]);
        }
    }
}

Oh, and I did not steal your bike. ;-)

Related