It is well known that missing initializers for an array of scalars are defaulted to zero.
int A[5]; // Entries remain uninitialized int B[5]= { 0 }; // All entries set to zero
But is this (below) guaranteed ?
int C[5]= { }; // All entries set to zero
Using default values in initialization of array For double or float , the default value is 0.0 , and the default value is null for string. Type[] arr = new Type[capacity]; For example, the following code creates a primitive integer array of size 5 . The array will be auto-initialized with a default value of 0 .
The array will be initialized to 0 in case we provide empty initializer list or just specify 0 in the initializer list. Designated Initializer: This initializer is used when we want to initialize a range with the same value.
The initializer for an array is a comma-separated list of constant expressions enclosed in braces ( { } ). The initializer is preceded by an equal sign ( = ). You do not need to initialize all elements in an array.
The empty braced initialisation performs aggregation-initialization of the array: this leads to zero-initialization of the int
elements.
Yes, this is guaranteed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With