arrayscstringnul

Why do we need a null terminator only in strings in C?


I'm taking CS50X. I'm on week 2 now. my question is: why do we need a null character '\0' in strings (aka null terminated char arrays) to mark its end, while we don't need it in normal char arrays or a non-string data type array such as an int array , like while printing both arrays (the null terminated char array and the int array for example) what does mark the end of the second array?

I tried to demonstrate how strings are implemented for myself with some code:

this code worked printing "hi!" in the terminal

this also worked printing the three scores

Why in the first code did we need an additional place in the array for the null character? Couldn't we have used i < 3 instead as we did in the second code? A character array, like any other array, has a specific length, so what changed when we decided to treat string as a character array?


Solution

  • Short answer: To be able to store short strings in a bigger array.

    Explanation:

    Assume you have (one way or another) allocated a memory area capable of holding M characters and you want to store a string into that memory.

    If the string has exactly M characters you can print it like:

    for (i = 0; i < M; ++i) putchar(str[i]);
    

    In principle it's not problem... You know the value M from the size of the memory area (note: this is only true in some cases but for now let's assume that).

    But what if you want to store and later print a string with N (N < M) characters in that memory?

    When printing it, you could of cause do:

    for (i = 0; i < N; ++i) putchar(str[i]);
    

    But from where do you get the value N?

    Sometimes N is 5 (e.g. the string "Hello"), sometimes N is 13 (e.g. the string "stackoverflow"), and so on.

    One solution would be to keep N in a separate variable that you update whenever you change the string.

    Another solution would be to use a sentinel value to indicate "End of string" and store that special value as part of the string.

    There are pros and cons in both solutions.

    The designers of C decided to go with the second solution. So consequently we must always make sure to include the sentinel (the NUL) when dealing with strings in C.

    The print can now be written:

    for (i = 0; str[i] != '\0'; ++i) putchar(str[i]);
    

    and it will work no matter what length the string has.

    BTW:

    Interesting read: https://stackoverflow.com/a/1258577/4386427