An array lets one name stand for many values of the same type, each reached by its position. This page covers the basic facts you need before anything else: declaration, size, bounds and initialisation.
What an array is
Because the elements are the same size and lie next to each other, the address of element i can be calculated directly: start address + i × element size. That is why reading or writing any element takes the same, constant time, O(1), whatever the index.
Declaring an array
The general form in C is:
Declaration of a one-dimensional array
data_type array_name[size];
data_type is the type of every element (int, char, float, a structure, …);
array_name is any valid identifier;
size is a positive integer constant expression: the number of elements.
For example, int a[100]; declares 100 integers, a[0] to a[99].
Memory and bounds
The memory needed is
bytes=size of one element×number of elements.
With 4-byte int, int a[100]; needs 4×100=400 bytes. In a program, write sizeof a for the total and sizeof a / sizeof a[0] for the number of elements; never assume a fixed size for int.
The smallest index is the lower bound (LB) and the largest is the upper bound (UB). In C the lower bound is always 0, so an array of n elements has indices 0 to n−1. In general (some languages and many exam questions allow other lower bounds):
number of elements=UB−LB+1.
Initialising an array
Initialisation examples
int a[4] = {34, 60, 93, 2}; /* size given and all values given */int b[] = {2, 3, 4, 5}; /* size left out: taken from the list (4) */int c[6] = {7, 8}; /* c[2] to c[5] become 0 */
If an array is initialised when it is declared, its size can be left out: the compiler counts the values. If fewer values are given than the size, the rest are set to 0.
What does an array contain if you give it no values at all? That depends on where it is declared:
Where declared
Initial value of elements
Inside a function, without static (automatic)
Indeterminate (“garbage”): reading it is an error
Outside all functions (global), or with static
0
With an initialiser list that is too short
The remaining elements are 0
No bounds checking
C does not check indices. Writing a[100] for an array of 100 elements compiles without error but uses memory that does not belong to the array: this is undefined behaviour, and it can crash the program or silently change other variables. Checking indices is the programmer’s job.
Key takeaways
An array is finite, ordered, homogeneous and contiguous; any element is reached in O(1).
Memory = element size × number of elements; number of elements = UB−LB+1.
In C, indices run from 0 to n−1; the size may be omitted when an initialiser list is given.
Missing initialisers become 0; only uninitialised local (automatic) arrays hold garbage.
C does not check bounds: an index outside the array is undefined behaviour.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Define an array. Write the formulas for its memory size and its number of elements, and apply them to float t[50]; (4-byte float).
Show worked answer
An array is a finite, ordered collection of elements of the same type, stored in contiguous memory and accessed by an integer index. Memory = size of one element × number of elements; number of elements = UB−LB+1. For float t[50];: LB=0, UB=49, so 49−0+1=50 elements, and memory =4×50=200 bytes.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-208/arrays-pointers-and-structures/one-dimensional-arrays/ to read this page.