Example 1 — Four initialization styles
Print each array to see what the compiler filled in.
#include <stdio.h>
static void show(const char *name, const int *a, int n) {
printf("%s:", name);
for (int i = 0; i < n; i++) printf(" %d", a[i]);
printf("\n");
}
int main(void) {
int b[5] = {1, 2};
int c[] = {3, 4, 5};
int d[5] = {[1] = 7, [3] = 9};
show("b", b, 5);
show("c", c, 3);
show("d", d, 5);
printf("c has %zu elements\n", sizeof c / sizeof c[0]);
return 0;
} How It Works
bsupplies two values; the rest default to 0.chas no size, so it gets size 3.- Designated initializers set only indexes 1 and 3.
