Let’s say I write char c[99] = {'Stack Overflow'}; in C or C++. It compiles fine but is this valid? By valid I meant not invoking any kind of undefined or unspecified behavior.
Again if I write char c[99] = 'Stack Overflow'; gcc complains about multicharacter constant which is obvious but in the above when I am enclosing within curly brackets compiler is happy! Why is it so?
I also notice that puts(c); after the first statement will output ‘w’ precisely the last character of a general string in-place of Stack Overflow. Why so?
Could somebody explain these behaviors separately?
They both are only a single literal, so
c[0]gets set to the literal andc[1]…c[98]get filled with zero (NUL character).I think what value actually gets stuffed into
c[0]is implementation dependent, but it should at least compile on any compliant compiler.EDIT: Verified against the standard, in C++0x at least:
And in C99 (using the draft, cause it’s free):