carraysstringchar

What is the correct string terminator in C?


As I know the string terminating character in C is '\0'.

Can we use '0' as the terminating character too? When I assign 0 to a specific index in a char array, and then use printf(), it prints only up to that specific index.

Hence, are both ways equivalent? Is the null character equal to the literal 0?


Solution

  • The only value that can be used as a null terminator is the numerical value 0.

    All strings literals implicitly contain the null terminator after their last visible character. In other cases, it may or may not be there automatically (depending on how the string was constructed), but you have to be sure that in every case the null terminator is there.