Arrays in C
An array in C is a collection of values of the same type stored in a single, contiguous block of memory, accessed by a numeric index. Instead of declaring score1, score2, score3, you declare one array scores[3] and reach each value with scores[0], scores[1], scores[2].
Declaring and initializing
You declare an array with a type, a name, and a fixed size:
int scores[5]; /* 5 ints, uninitialized */
int marks[3] = {90, 75, 60}; /* declared and initialized */
int zeros[4] = {0}; /* all elements set to 0 */
int auto_sized[] = {1, 2, 3, 4}; /* size inferred as 4 */
The size must be known when you declare it (a constant), and it cannot change later. Indexing starts at 0, so an array of size 5 has valid indices 0 through 4.
Reading and writing elements
#include <stdio.h>
int main(void) {
int marks[3] = {90, 75, 60};
marks[1] = 80; /* change the second element */
printf("First: %d\n", marks[0]); /* 90 */
printf("Second: %d\n", marks[1]); /* 80 */
return 0;
}
Looping over an array
Arrays and for loops are a natural pair. To visit every element, loop from 0 to size minus one:
#include <stdio.h>
int main(void) {
int data[5] = {12, 7, 25, 3, 18};
int sum = 0;
for (int i = 0; i < 5; i++) { /* i goes 0,1,2,3,4 */
sum += data[i]; /* add each element */
}
printf("Total: %d\n", sum); /* 65 */
return 0;
}
Want to learn this properly?
Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.
Explore C ProgrammingMultidimensional arrays
A 2D array is an array of arrays — useful for grids, matrices, and tables:
#include <stdio.h>
int main(void) {
int grid[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
for (int r = 0; r < 2; r++) { /* rows */
for (int c = 0; c < 3; c++) { /* columns */
printf("%d ", grid[r][c]);
}
printf("\n");
}
return 0;
}
Finding the largest element
A very common array task is scanning for a maximum or minimum. Start by assuming the first element is the largest, then compare each remaining element against your running best:
#include <stdio.h>
int main(void) {
int data[5] = {12, 7, 25, 3, 18};
int max = data[0]; /* assume the first is largest */
for (int i = 1; i < 5; i++) { /* start at index 1 */
if (data[i] > max) {
max = data[i]; /* found a bigger one */
}
}
printf("Largest: %d\n", max); /* 25 */
return 0;
}
The same pattern — initialize from the first element, then loop and compare — finds the minimum, the sum, or the average.
Arrays and memory
Array elements sit next to each other in memory, which makes them fast to scan. The array name often "decays" to a pointer to its first element when passed to a function — this links closely to pointers. A string in C is simply an array of char ending in a null byte; see Strings in C.
Common mistakes
- Out-of-bounds access:
int a[5];thena[5]is invalid — valid indices stop at4. C does not check this for you; it leads to undefined behaviour and crashes. - Off-by-one in loops: use
i < size, noti <= size. - Forgetting arrays are zero-indexed: the first element is
[0], not[1]. - Trying to resize a fixed array: its size is set at declaration. For a resizable collection, use dynamic memory.
- Assuming uninitialized arrays are zero: local arrays hold garbage until you set them.
FAQ
How do I find an array's length? For a local array, sizeof(arr) / sizeof(arr[0]). This does not work once the array is passed to a function (it becomes a pointer); pass the length separately.
Can arrays hold mixed types? No — every element must be the same type. Use a struct array to group different fields.
Continue at the hub C Programming, then Strings in C and Pointers in C.
Want to learn this properly? Join the waitlist for our C Programming course — taught in Jalgaon.
Want to learn this properly?
Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.
Explore C ProgrammingFounder, Infoplanet
Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.
Related guides
Common C Errors & How to Fix Them
A troubleshooting guide to the most common C errors beginners face — from = vs ==, format mismatches and missing semicolons to segmentation faults — and how to fix each.
Dynamic Memory Allocation in C
A beginner guide to dynamic memory allocation in C — malloc, calloc, realloc and free, allocating at runtime, and preventing memory leaks.
File Handling in C
A beginner guide to file handling in C — opening files with fopen, file modes, writing with fprintf, reading with fgets, and always closing with fclose.
