I was asked what this syntax means:
char data[32] = {1};
However I can't remember what it does,. but I remember that it's valid.
Can anyone explain its purpose ?
I was asked what this syntax means:
char data[32] = {1};
However I can't remember what it does,. but I remember that it's valid.
Can anyone explain its purpose ?
This will create an array of 32 chars. The first element will be 1 and the rest will be set to zero.
If you do not initialize an automatic array (local and not static) at all, the elements of the array may contain garbage values.
#include <stdio.h>
int main(int argc, char **argv)
{
int i;
char data[5];
for (i = 0; i < 5; i++) {
printf("data[%d] = %d\n", i, data[i]);
}
return 0;
}
Output:
data[0] = 72
data[1] = 12
data[2] = -118
data[3] = 51
data[4] = -119
However, if you do initialize it, it will be initialized with the initializers you have specified. If the number of initializers is less than the total number of the elements in the array, the remaining elements will be initialized to 0.
#include <stdio.h>
int main(int argc, char **argv)
{
int i;
char data[5] = {1};
for (i = 0; i < 5; i++) {
printf("data[%d] = %d\n", i, data[i]);
}
return 0;
}
Output:
data[0] = 1
data[1] = 0
data[2] = 0
data[3] = 0
data[4] = 0
It is initialisation. It sets the first element of the array of a value of 1
(^A
in the ASCII table I checked) and the remainder to 0
(default).
To state the C99 standard on omitted initializers:
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:
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 pointer type, it is initialized to a null pointer;
- if it has arithmetic type, it is initialized to (positive or unsigned) zero;
- if it is an aggregate, every member is initialized (recursively) according to these rules;
- if it is a union, the first named member is initialized (recursively) according to these rules.
And char
is an arithmetic type, so the remaining elements of the array will be initialised to zero.
Yes, you recall correctly. Just Google for "C array initialization":
A couple of notes:
Under older, pre-ANSI C compilers, you could not always supply initializers for "local" arrays inside functions; you could only initialize "global" arrays, those outside of any function. Those compilers are now rare, so you shouldn't have to worry about this distinction any more.
When an array definition includes an initializer, the array dimension may be omitted, and the compiler will infer the dimension from the number of initializers
If there are fewer initializers than elements in the array, the remaining elements are automatically initialized to 0.