An array in C is one of the most important building blocks in the language. It allows a program to store multiple values of the same data type under one name and access those values using an index. Instead of creating many separate variables such as marks1, marks2, marks3, and so on, an array lets us group related values in an organized and efficient form.
Arrays are used everywhere in C programming because they are simple, fast, and directly connected to how memory works. If you understand arrays properly, later topics such as strings, pointers, matrices, function arguments, and dynamic memory become much easier. In this article, we will understand array in C, its syntax, declaration, initialization, memory behavior, examples, common mistakes, and best practices.
What is Array in C?
An array in C is a collection of elements of the same data type stored in contiguous memory locations. Each element is identified by an index, and indexing starts from 0.
For example, if we declare an integer array of size 5, the program reserves space for 5 integers in consecutive memory positions. We can then access each value using its position number such as arr[0], arr[1], and arr[2].
An array stores many values of one type in one continuous block of memory.
Why Arrays are Used in C
Arrays are used when a program must handle multiple values of the same kind in an efficient way. They reduce repetition and make loops much more useful.
- to store marks of many students
- to process a list of numbers
- to hold characters in strings
- to represent matrices and tables
- to pass grouped data to functions
- to work with memory efficiently in low-level code
Syntax of Array Declaration in C
The general syntax of declaring an array in C is:
data_type array_name[size];Example:
int marks[5];
float temperature[7];
char name[20];Here, marks stores 5 integers, temperature stores 7 floating-point values, and name stores 20 characters.
| Part | Meaning |
|---|---|
data_type | Type of each element such as int, float, or char |
array_name | Name used to refer to the array |
size | Total number of elements the array can hold |
Initialization of Array in C
An array can be initialized at the time of declaration or filled later in the program.
Initialization at declaration:
int numbers[5] = {10, 20, 30, 40, 50};Size can also be omitted if values are provided:
int numbers[] = {10, 20, 30, 40, 50};Partial initialization is also valid. Remaining elements become zero for this kind of initialization.
int values[5] = {1, 2};After this, values[0] is 1, values[1] is 2, and the remaining elements are 0.
Accessing Array Elements in C
Each array element is accessed by its index. Since indexing starts from 0, the first element is arr[0] and the last element of an array of size n is arr[n - 1].
#include <stdio.h>
int main(void)
{
int marks[3] = {85, 90, 78};
printf("%d\n", marks[0]);
printf("%d\n", marks[1]);
printf("%d\n", marks[2]);
return 0;
}This program prints the three stored values one by one.
Modifying Array Elements in C
Array elements can be changed by assigning a new value to a valid index.
int marks[3] = {85, 90, 78};
marks[1] = 95;After this assignment, the second element changes from 90 to 95.
Memory Representation of Array in C
One of the most important things about arrays in C is that their elements are stored in contiguous memory locations. This means each element is placed right next to the previous one in memory.
If an int takes 4 bytes and an array contains 5 integers, the total memory required is usually 5 × 4 = 20 bytes.
| Element | Example index | Common size per element (bytes) |
|---|---|---|
arr[0] | 0 | 4 for int |
arr[1] | 1 | 4 for int |
arr[2] | 2 | 4 for int |
arr[3] | 3 | 4 for int |
arr[4] | 4 | 4 for int |
This contiguous layout is the reason array traversal is fast and why arrays are closely related to pointers in C.
Array and Indexing in C
Indexing is zero-based in C. This is one of the first places where beginners make mistakes.
- The first element is
arr[0]. - The second element is
arr[1]. - The last valid index of an array of size 5 is
4.
If you try to access arr[5] in an array of size 5, the result is out-of-bounds access. This is undefined behavior and may produce garbage values or crash the program.
Traversing Array in C Using Loop
A loop is the most common way to process all elements of an array.
#include <stdio.h>
int main(void)
{
int numbers[5] = {10, 20, 30, 40, 50};
int i;
for (i = 0; i < 5; i++)
{
printf("%d ", numbers[i]);
}
return 0;
}This pattern is extremely common and appears in searching, sorting, copying, summing, and many other array-related tasks.
Types of Arrays in C
The term array in C usually starts with one-dimensional arrays, but arrays can have more than one dimension.
| Type | Description | Example |
|---|---|---|
| One-dimensional array | A simple linear collection of values | int a[5]; |
| Two-dimensional array | Array arranged like rows and columns | int m[3][4]; |
| Multidimensional array | Array with more than two dimensions | int x[2][3][4]; |
This article focuses mainly on the basic one-dimensional array, while 2D and multidimensional arrays are covered as separate topics.
Array vs Pointer in C
Arrays and pointers are closely related, but they are not the same thing.
| Point | Array | Pointer |
|---|---|---|
| Meaning | Collection of same-type elements | Variable that stores an address |
| Storage | Memory for all elements is reserved together | Stores only an address value |
| Assignability | Array name itself cannot be reassigned | Pointer variable can point elsewhere |
| Use | Store multiple values | Work with addresses and indirect access |
In many expressions, the array name behaves like the address of the first element, which is why arrays and pointers often appear together in C discussions. But conceptually they should not be treated as identical.
Finding Size of Array Using sizeof
The sizeof operator is commonly used to find the total size of an array in bytes. From that, we can calculate the number of elements.
#include <stdio.h>
int main(void)
{
int numbers[5] = {10, 20, 30, 40, 50};
int total_bytes = sizeof(numbers);
int total_elements = sizeof(numbers) / sizeof(numbers[0]);
printf("Total bytes = %d\n", total_bytes);
printf("Total elements = %d\n", total_elements);
return 0;
}This method works well when the array is still available as an actual array, not after it has decayed to a pointer in a function parameter.
Common Mistakes with Array in C
- using index
1for the first element instead of0 - accessing beyond the last valid index
- forgetting to initialize elements before use
- mixing up array size with last index
- assuming arrays and pointers are exactly the same
- using wrong loop limits while traversing the array
| Mistake | Problem | Better practice |
|---|---|---|
Accessing arr[5] in size 5 array | Undefined behavior | Use indices only from 0 to size - 1 |
| Wrong loop limit | Missing elements or out-of-bounds access | Loop carefully with correct size |
| Uninitialized array values | Garbage data may be used | Initialize before reading |
| Confusing pointer with array | Conceptual errors | Understand the difference clearly |
Best Practices for Array in C
- Always keep track of array size.
- Use clear loop boundaries when traversing arrays.
- Initialize arrays before reading values.
- Use
sizeofcarefully when the array is available in the same scope. - Do not access elements outside valid index range.
- Use meaningful names such as
marks,temperatures, orscores.
FAQs
What is array in C?
An array in C is a collection of elements of the same data type stored in contiguous memory locations and accessed using an index.
Why does array indexing start from 0 in C?
C uses zero-based indexing because array access is closely related to address calculation from the first element.
Can we change array elements in C?
Yes, array elements can be changed using valid indices such as arr[2] = 50;.
What is the difference between array and pointer in C?
An array stores multiple values in contiguous memory, while a pointer stores the address of a memory location.
How do you find the number of elements in an array in C?
A common method is sizeof(array) / sizeof(array[0]), as long as the value is still an actual array in the same scope.
What happens if array index goes out of bounds in C?
It causes undefined behavior, which may produce garbage values, corrupt memory, or crash the program.