I'm using gcc 4.9.1/Mingw and compiling the code with:
gcc test.c -otest.exe -std=c11 -pedantic-errors -Wall -Wextra
This code gives a diagnostic:
int main (void)
{
char a[5] = {'h','e','l','l','o',''};
}
error: excess elements in array initializer char a[5]
However, this code does not yield a warning:
int main (void)
{
char b[5] = "hello";
}
I thought the two forms were 100% equivalent. Is there any reason or subtlety in the C standard etc why the latter should not give a warning?
Or is this a compiler bug? I know that the C standard allows excess initializers, unlike C++, so formally I don't believe gcc is required to give a diagnostic. But I would expect the compiler to give warnings consistently.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…