(Courtesy Pascal Cuoq)
The C99 standard §6.7.8.¶14 says
An array of character type may be initialized by a character string
literal, optionally enclosed in braces. Successive characters of the
character string literal (including the terminating null character if
there is room or if the array is of unknown size) initialize the
elements of the array.
Since strings are terminated by the null byte '\0'
, the actual number of characters in the string literal "abcd"
is 5
. The size of the arrays str1
and str2
is 4
. Therefore, they cannot hold the null byte and are, in fact, not strings. The below statements are equivalent.
char str1[4] = "abcd";
char str1[4] = {"abcd"}
char str1[4] = {'a', 'b', 'c', 'd'};
Passing str1
and str2
to strcmp
invokes undefined behaviour because they are not strings. strcmp
will overrun the buffer pointed to by str1
and str2
since it cannot find the terminating null byte. This is undefined behaviour and may result in program crash due to segfault.
A string is a character array terminated by the null byte '\0'
. Therefore, the length of the string literal "abcd"
is 5
and not 4
. Note that the standard library function strlen
does not count the null byte so in this case, strlen("abcd")
returns 4
.
When you initialize an array with a string literal, it is a good practice to leave your array size blank which is automatically determined to be large enough to store all the characters in the string literal it is initialized with.
#include <stdio.h>
#include <string.h>
int main(void) {
char str1[] = "abcd";
char str2[] = "abcd";
printf("%d\n", strcmp(str1, str2)); // prints 0
return 0;
}