Search code examples
cinitializationc-stringsstrlen

Utility of '\0' in C string


#include <stdio.h>
#include <string.h>
int main()
{
    char ch[20] = {'h','i'};
    int k=strlen(ch);
    printf("%d",k);
    
    return 0;
} 

The output is 2.

As far as I know '\0' helps compiler identify the end of string but the output here suggests the strlen can detect the end on it's own then why do we need '\0'?


Solution

  • long story short: it's your compiler making proactive decisions based on the standard.

    long story:

    char ch[20] = {'h','i'}
    

    in the line above what you are implying to your compiler is;

    • allocate a memory big enough to store 20 characters (aka, array of 20 chars).
    • initialize first two slices (first two members of the array) as 'h' & 'i'.
    • implicitly initialize the rest.

    since you are initialing your char array, your compiler is smart enough to insert the null terminator to the third element if it has enough space remaining. This process is the standard for initialization.

    if you were to remove the initialization syntax and initialize each member manually like below, the result is undefined behavior.

    char ch[20];
    ch[0] = 'h';
    ch[1] = 'i';
    

    Also, if you were to not have extra space for your compiler to put the null terminator, even if you used a initializer the result would still be an undefined behavior as you can easily test via this code snippet below:

    char ch[2] = { 'h','i' };
    
    int k = strlen(ch);
    printf("%d\n%s\n", k, ch);
    

    now, if you were to increase the array size of 'ch' from 2 to 3 or any other number higher than 2, you can see that your compiler initializes it with the null terminator thus no more undefined behavior.