3

does this code initialize all the elements of the array in all major C and C++ compilers or not?

int arr[100] = {0};
1
  • 1
    That's the same as = {}, but not the same as = {1} or anything else which is not zero. Commented Nov 9, 2012 at 0:12

2 Answers 2

6

In all compilers. This is guaranteed by the C Standard and the C++ Standard.

For example, for C here is the relevant paragraph:

(C99, 6.7.8p21) "If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration."

and

(C99, 6.7.8p10) "If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then: [...] if it has arithmetic type, it is initialized to (positive or unsigned) zero; [...]"

Sign up to request clarification or add additional context in comments.

3 Comments

aha, tnx. then what is the result of int arr[100] = {1}?
@MBZ first element of the array is initialized to 1, all remaining elements initialized to 0.
@Potatoswatter I think you should read the quote again. It never assumes the array has static storage duration. The quote basically specifies that if an array has fewer initializers than the number of array elements, the remaining elements are initialized to their default value which is the one used in an static array. For a static int element, the default value is 0.
1

If the array happens to be an array of structs, you'll quickly run into problems. Heck, you can't even do a single struct.

This simple example shows that two major compilers (gcc and msvc) do not follow the specifications as quoted in ouah's answer (gcc seemingly, from its warning, and msvc from its error msg).

Consider this source, foo.c / foo.cpp:

void foo(void) {
   struct A {
      int i;
      int j;
   };
   struct A single = {0};
}

Compile, with both gcc and g++:

$ gcc -Wall -W -c foo.c
foo.c: In function 'foo':
foo.c:6:14: warning: missing initializer
foo.c:6:14: warning: (near initialization for 'single.j')

$ g++ -Wall -W -c foo.cpp
foo.cpp: In function 'void foo()':
foo.cpp:6:27: warning: missing initializer for member 'foo()::A::j'

The same warnings are given for arrays. This gcc is only two years old: gcc --version --> gcc (GCC) 4.5.3.

Plain {} without the 0 works fine in gcc, including for arrays. And you could always compile with -w (lower case) to disable all warnings.

But MSVS 2012 has the equal and opposite problem with this example (including arrays).

It likes {0}, and treats {} as an error :

Error C2059: syntax error : '}'

5 Comments

Sorry, I don't quite understand. In what way doesn't GCC follow the specification?
@jogojapan: I am assuming that GCC does in fact follow the specifications. I should have made this clearer. I did have an earlier quip about the terrible -w switch. But this post is only about warnings, not about real code generation (except in case of msvc).
Perhaps you overlooked this when you edited the question, but it still says two major compilers (gcc and msvc) do not follow the specifications. Did you intent to change this? (I'd do it myself if I was sure that this is what you intended.)
@jogojapan: Correction noted. I am not an expert, and I am willing to accept guidance.
I am no expert either... anyway, as far as I can tell, a warning is a good thing. It demonstrates that the compiler does what the Standard says, but makes the user aware that this is likely not what the user intended.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.