Nearby lessons

54 of 124

C - Array Initialization

Every way to initialise an array in C — full lists, partial lists, letting the compiler count the size, zeroing an array with {0}, designated initialisers, and assigning element by element.

Full Initialisation

List every value inside braces, separated by commas. The count must not exceed the declared size:

Example01
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int marks[5] = {85, 90, 78, 92, 88};
6 float rates[3] = {1.5, 2.25, 3.0};
7 char vowels[5] = {'a', 'e', 'i', 'o', 'u'};
8 int i;
9 
10 for (i = 0; i < 5; i++) printf("%d ", marks[i]);
11 printf("\n");
12 for (i = 0; i < 3; i++) printf("%.2f ", rates[i]);
13 printf("\n");
14 for (i = 0; i < 5; i++) printf("%c ", vowels[i]);
15 printf("\n");
16 return 0;
17}
Output
85 90 78 92 88
1.50 2.25 3.00
a e i o u 

Partial Initialisation — The Rest Become 0

If you supply fewer values than the size, C fills the remainder with zero. This is guaranteed by the standard, not luck:

Example02
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int a[5] = {10, 20}; /* becomes {10, 20, 0, 0, 0} */
6 float f[4] = {1.5}; /* becomes {1.5, 0.0, 0.0, 0.0} */
7 int i;
8 
9 for (i = 0; i < 5; i++) printf("%d ", a[i]);
10 printf("\n");
11 for (i = 0; i < 4; i++) printf("%.1f ", f[i]);
12 printf("\n");
13 return 0;
14}
Output
10 20 0 0 0
1.5 0.0 0.0 0.0 

Let the Compiler Count

Leave the brackets empty and the compiler sizes the array from the list. Now adding a value cannot desynchronise the size:

Example03
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int nums[] = {10, 20, 30, 40, 50, 60}; /* size becomes 6 */
6 int size = sizeof(nums) / sizeof(nums[0]);
7 int i;
8 
9 printf("Size deduced: %d\n", size);
10 for (i = 0; i < size; i++) printf("%d ", nums[i]);
11 printf("\n");
12 return 0;
13}
Output
Size deduced: 6
10 20 30 40 50 60 

Zeroing an Entire Array

The idiom {0} sets the first element to 0 and — by the partial-initialisation rule — every other element to 0 as well:

Example04
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int counts[10] = {0}; /* all 10 elements are 0 */
6 char buffer[20] = {0}; /* an empty string */
7 int i;
8 
9 for (i = 0; i < 10; i++) printf("%d ", counts[i]);
10 printf("\nbuffer as string: [%s]\n", buffer);
11 return 0;
12}
Output
0 0 0 0 0 0 0 0 0 0
buffer as string: []

Designated Initialisers (C99)

You can name the index you want to set. Everything you skip becomes 0 — very handy for sparse arrays:

Example05
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int a[10] = {[0] = 5, [4] = 50, [9] = 99};
6 int i;
7 
8 for (i = 0; i < 10; i++)
9 printf("a[%d] = %d\n", i, a[i]);
10 return 0;
11}
Output
a[0] = 5
a[1] = 0
a[2] = 0
a[3] = 0
a[4] = 50
a[5] = 0
a[6] = 0
a[7] = 0
a[8] = 0
a[9] = 99

Initialising After Declaration

The brace list only works at the moment of declaration. Later on you must assign element by element, usually in a loop:

Example06
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int a[5];
6 int i;
7 
8 /* a = {1,2,3,4,5}; INVALID - cannot assign to an array */
9 
10 a[0] = 10; /* element by element */
11 a[1] = 20;
12 
13 for (i = 2; i < 5; i++) /* or with a loop */
14 a[i] = (i + 1) * 10;
15 
16 for (i = 0; i < 5; i++) printf("%d ", a[i]);
17 printf("\n");
18 return 0;
19}
Output
10 20 30 40 50 

Filling an Array from User Input

The most common real pattern: declare, then fill with a loop and scanf:

Example07
CCode Cell
1#include <stdio.h>
2 
3#define SIZE 5
4 
5int main()
6{
7 int marks[SIZE];
8 int i, total = 0;
9 
10 for (i = 0; i < SIZE; i++)
11 {
12 printf("Enter mark %d: ", i + 1);
13 scanf("%d", &marks[i]); /* & needed - marks[i] is an int */
14 total += marks[i];
15 }
16 
17 printf("Average: %.2f\n", (float) total / SIZE);
18 return 0;
19}
Output
Enter mark 1: 80
Enter mark 2: 75
Enter mark 3: 90
Enter mark 4: 85
Enter mark 5: 70
Average: 80.00

Common Mistakes

MistakeWhy it fails
int a[3] = {1,2,3,4};Too many initialisers — compiler error
int a[]; No size and no list — compiler cannot size it
a = {1,2,3}; after declaringArrays are not assignable
b = a; to copy an arrayNot allowed — copy with a loop or memcpy
if (a == b) to compareCompares addresses, not contents
int a[100] = {1}; does not fill the array with 1. It sets a[0] = 1 and the other 99 elements to 0. Only {0} does what people expect, because the fill value happens to be 0 too.
📝 Key Takeaways
  • int a[5] = {1,2,3,4,5}; initialises all elements.
  • Missing values are filled with 0 automatically.
  • int a[] = {1,2,3}; lets the compiler count — size becomes 3.
  • int a[100] = {0}; zeroes the whole array.
  • An array cannot be assigned with = after declaration.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4