I expected to see "initializer-string for array of chars is too long" warning for both of the variables in the following program using gcc.
Program:
int main()
{
char str1[4]="1234";
char str2[3]="1234";
(void)str1; // Remove unused variable warning.
(void)str2; // Remove unused variable warning.
return 0;
}
However, I got a warning only for str2
.
Since
char str1[4]="1234";
is equivalent to
char str1[4]= {'1', '2', '3', '4', '\0'};
shouldn't we get the same warning for str1
also?
Is this a defect in gcc?
Compiler command:
gcc -Wall -std=c99 soc.c -o soc
gcc
version is 4.8.4.
Update
Learned just now that
char str1[4]="1234";
is not equivalent to
char str1[4]= {'1', '2', '3', '4', '\0'};
Update 2
char str1[4]="1234";
is ill-formed in C++11 (Section 8.5.2/2). I didn't think C99 and C++11 would treat them differently.