Nearby lessons

53 of 124

C - Array Declaration

How to declare an array in C — the type name[size] syntax, the rules for the size expression, declaring several arrays at once, and what an uninitialised array actually contains.

The Declaration Syntax

An array declaration has exactly three parts:

PartExampleMeaning
Data typeintWhat kind of value each element holds
Array namemarksAny valid identifier
Size in brackets[5]How many elements to reserve
Example01
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int marks[5]; /* 5 ints = 20 bytes */
6 float prices[10]; /* 10 floats = 40 bytes */
7 char name[50]; /* 50 chars = 50 bytes */
8 double values[3]; /* 3 doubles = 24 bytes */
9 
10 printf("marks : %zu bytes\n", sizeof(marks));
11 printf("prices : %zu bytes\n", sizeof(prices));
12 printf("name : %zu bytes\n", sizeof(name));
13 printf("values : %zu bytes\n", sizeof(values));
14 return 0;
15}
Output
marks  : 20 bytes
prices : 40 bytes
name   : 50 bytes
values : 24 bytes

Rules for the Size

In classic C the size must be known when the program is compiled, because the compiler has to reserve the exact number of bytes:

Example02
CCode Cell
1#include <stdio.h>
2 
3#define MAX 100 /* macro constant - preferred */
4 
5int main()
6{
7 int a[10]; /* OK - integer literal */
8 int b[MAX]; /* OK - macro constant */
9 int c[5 * 2]; /* OK - constant expression */
10 
11 /* int d[0]; INVALID - size must be positive */
12 /* int e[-5]; INVALID - negative size */
13 /* int f[2.5]; INVALID - must be an integer */
14 
15 printf("a has %zu elements\n", sizeof(a) / sizeof(a[0]));
16 printf("b has %zu elements\n", sizeof(b) / sizeof(b[0]));
17 printf("c has %zu elements\n", sizeof(c) / sizeof(c[0]));
18 return 0;
19}
Output
a has 10 elements
b has 100 elements
c has 10 elements

Using a Variable as the Size

Since C99, a local array may be sized by a variable — this is called a variable length array (VLA). It works on most compilers, but it is optional in C11 and cannot be initialised with a list:

Example03
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int n = 5;
6 int arr[n]; /* VLA - C99 onwards */
7 int i;
8 
9 for (i = 0; i < n; i++)
10 arr[i] = i * 10;
11 
12 for (i = 0; i < n; i++)
13 printf("%d ", arr[i]);
14 printf("\n");
15 return 0;
16}
Output
0 10 20 30 40 

Prefer #define or const

Writing the size in one place means you change it in one place. Compare these two versions:

Example04
CCode Cell
1#include <stdio.h>
2 
3#define SIZE 5
4 
5int main()
6{
7 int marks[SIZE];
8 int i;
9 
10 for (i = 0; i < SIZE; i++) /* loop bound stays in sync */
11 marks[i] = (i + 1) * 10;
12 
13 for (i = 0; i < SIZE; i++)
14 printf("%d ", marks[i]);
15 printf("\n");
16 return 0;
17}
Output
10 20 30 40 50 

Declaring Several Arrays Together

Arrays of the same type can share one declaration, and you can mix arrays with ordinary variables:

Example05
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int a[5], b[10], c[3]; /* three int arrays */
6 int count = 0, temp[4]; /* a variable and an array */
7 
8 printf("a: %zu ints\n", sizeof(a) / sizeof(a[0]));
9 printf("b: %zu ints\n", sizeof(b) / sizeof(b[0]));
10 printf("c: %zu ints\n", sizeof(c) / sizeof(c[0]));
11 printf("temp: %zu ints, count = %d\n",
12 sizeof(temp) / sizeof(temp[0]), count);
13 return 0;
14}
Output
a: 5 ints
b: 10 ints
c: 3 ints
temp: 4 ints, count = 0

What Is Inside an Undeclared-Value Array?

Declaring an array reserves the bytes but does not clear them. A local array starts out holding whatever was left in that memory:

Example06
CCode Cell
1#include <stdio.h>
2 
3int global[3]; /* globals ARE zero-initialised */
4 
5int main()
6{
7 int local[3]; /* locals are NOT - garbage values */
8 int i;
9 
10 printf("global: ");
11 for (i = 0; i < 3; i++) printf("%d ", global[i]);
12 
13 printf("\nlocal : ");
14 for (i = 0; i < 3; i++) printf("%d ", local[i]);
15 printf("\n");
16 return 0;
17}
Output
global: 0 0 0
local : 4200880 0 6422296 

Common Mistakes

  • Using the size as an indexint a[5]; then a[5] = 1; writes out of bounds.
  • Empty brackets without an initialiserint a[]; is an error; the compiler cannot guess the size.
  • Assuming locals start at zero — they do not. Initialise before you read.
  • Declaring a huge array as a localint big[10000000]; overflows the stack and crashes. Large arrays belong in global scope or on the heap.
Brackets go after the name, not the type. Write int marks[5]; — not int[5] marks;. The second form is Java or C#, and it will not compile in C.
📝 Key Takeaways
  • Syntax: dataType arrayName[size];
  • The size must be a positive integer constant, not a variable.
  • Use #define or const for the size to avoid magic numbers.
  • An uninitialised local array contains garbage values.
  • Declaring an array reserves size * sizeof(type) bytes.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4