Expected to see "initializer-string for array of chars is too long" warning

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.


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.


   char str1[4]="1234";

is equivalent to

   char str1[4]= {'1', '2', '3', '4', '{$content}'};

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.


Learned just now that

   char str1[4]="1234";

is not equivalent to

   char str1[4]= {'1', '2', '3', '4', '{$content}'};

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.

Source: gcc

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.