Introduction to Arrays in C Programming
Learn how arrays work in C, from declaration and initialisation to 1D and 2D types, core operations, and the patterns that appear in placement interviews.
An array in C stores multiple values of the same type in adjacent memory slots, accessible by a numeric index that starts at zero. That single fact is all you need to reason through most interview questions on the topic. The data structure appears in every standard C curriculum and in nearly every placement screening test, because arrays are the foundation for the algorithms freshers are most often asked to implement: linear search, sorting, and matrix manipulation.
What an Array Is (and What It Is Not)
An array is a contiguous block of memory holding a fixed number of elements, all of the same data type. “Contiguous” means the elements sit next to each other in memory with no gaps between them. “Fixed” means the size is set at declaration and cannot change at runtime in standard C.
When you write int scores[5], the compiler reserves 20 bytes: 5 elements at 4 bytes per int on most 32-bit and 64-bit systems. The element scores[0] starts at the base address, scores[1] is 4 bytes later, scores[2] is 8 bytes later, and so on. Because every element is at a predictable offset from the base, any element can be reached in constant time: access time is O(1), regardless of array size.
According to the cppreference documentation on C arrays, the array type in C is deliberately minimal. It is a block of memory with an element type and a size, nothing more. There is no built-in length field, no bounds checking, and no automatic resizing. This minimalism is why arrays are fast and why they are also easy to misuse.
What an array is not: it is not a list that grows on demand, not a set that prevents duplicates, and not a structure that tracks how many elements are currently in use. The programmer manages all of those concerns explicitly. That responsibility is also why off-by-one errors are so common with arrays, and why interviewers specifically test for them.
Declaring and Initialising Arrays in C
Declaration Syntax
The general form for declaring an array is:
data_type array_name[array_size];
Examples:
int scores[5]; /* 5 integers */
float prices[10]; /* 10 floating-point values */
char name[20]; /* 20 characters */
A declared-but-not-initialised array inside a function holds garbage values: whatever bits happen to sit in that memory region. A declared-but-not-initialised array at global scope is automatically zeroed by the C runtime before main() runs.
Initialisation at Declaration
You can provide initial values with a brace-enclosed list:
int scores[5] = {85, 90, 78, 92, 88};
If you provide fewer values than the declared size, the remaining elements are set to zero:
int arr[5] = {1, 2};
/* arr[0]=1, arr[1]=2, arr[2]=0, arr[3]=0, arr[4]=0 */
If you list all values at once, you can omit the size and let the compiler count for you:
int arr[] = {10, 20, 30, 40, 50};
/* size inferred as 5 */
Three common early errors: initialising with more values than the declared size (a compile-time error), relying on zero-initialisation for local arrays that have no explicit initialiser (undefined behaviour), and confusing the array size with the last valid index (size 5 means valid indices are 0 through 4, not 0 through 5).
Types of Arrays in C
One-Dimensional Arrays
A 1D array is a single linear sequence of elements. This is the form that appears in most placement test questions.
int arr[4] = {5, 10, 15, 20};
printf("%d\n", arr[2]); /* prints 15 */
The index 2 retrieves the third element, because indexing starts at 0. Most traversal, search, and sorting questions use 1D arrays directly.
Two-Dimensional Arrays
A 2D array organises elements in rows and columns, which makes it useful for representing matrices and grids.
int matrix[3][3]; /* 3 rows, 3 columns — 9 elements total */
In C, 2D arrays use row-major storage: all elements of row 0 sit in memory first, then all of row 1, and so on. This layout affects performance when you iterate: iterating column-by-column on a large 2D array causes more cache misses than iterating row-by-row.
Initialisation groups elements by row:
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
printf("%d\n", matrix[1][2]); /* prints 6 (row 1, column 2) */
The GeeksforGeeks reference on arrays in C covers multi-dimensional initialisation patterns in detail if you need additional examples. In placement tests, 2D arrays appear in questions on matrix transpose, row and column sums, saddle-point detection, and spiral traversal.
Three-Dimensional Arrays
A 3D array stores multiple 2D matrices:
int cube[2][3][4]; /* 2 blocks, each a 3x4 matrix — 24 elements total */
3D arrays rarely appear in fresher-level placement tests. Understand the syntax, but concentrate prep time on 1D and 2D.
Core Array Operations in C
These four operations cover the majority of what placement screening tests ask freshers to implement.
Traversal
Iterate through every element using a loop:
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
/* Output: 10 20 30 40 50 */
Time complexity: O(n). Traversal is the inner logic of most other array operations. Once you understand it, search and sort are straightforward extensions.
Linear Search
Scan each element in sequence until the target is found or the array is exhausted:
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int key = 30, found = 0;
for (int i = 0; i < 5; i++) {
if (arr[i] == key) {
printf("Found at index %d\n", i);
found = 1;
break;
}
}
if (!found) {
printf("Not found\n");
}
return 0;
}
/* Output: Found at index 2 */
Time complexity: O(n) worst case. Binary search is faster at O(log n), but it requires the array to be sorted first. Linear search works on any array in any order.
Deletion by Shifting
C has no built-in delete operation for arrays. To remove an element at position pos, shift every element after it one slot to the left, then decrement the logical size counter:
/* Removes element at index pos; size is the current element count */
for (int i = pos; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
size--;
The physical array remains the same length in memory. The variable size tracks how many elements are logically active. Removing the first element costs O(n) shifts; removing the last element costs O(1).
Sorting: Bubble Sort
Bubble sort repeatedly compares adjacent elements and swaps them if they are out of order, pushing larger values toward the end on each pass:
#include <stdio.h>
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[5] = {50, 20, 40, 10, 30};
bubbleSort(arr, 5);
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
/* Output: 10 20 30 40 50 */
Time complexity: O(n²) comparisons in the worst and average cases. The standard library qsort() is faster for production code, but placement tests routinely ask for a hand-written sort to check whether you understand nested loops and in-place swapping.
Arrays in Placement Interviews
Three patterns appear often enough to memorise:
- Reverse an array in place. Two-pointer approach: swap
arr[0]witharr[n-1], thenarr[1]witharr[n-2], continuing until the pointers meet in the middle. Time: O(n), no extra array needed. - Find maximum and minimum. Single traversal, tracking max and min with two variables initialised to
arr[0]. Time: O(n). - Count or remove duplicates. Sort first, then scan for adjacent equal elements. Alternatively, use a frequency array when the value range is small and known in advance.
Two mistakes that interviewers specifically watch for:
- Using
i <= nas a loop condition instead ofi < n. For an array of size n, valid indices run from 0 to n-1. The index n is out-of-bounds. - Accessing
arr[n]when checking a boundary condition. This is undefined behaviour, and it is covered with fixes in the FACE Prep guide to common errors in C programming.
Arrays and pointers are connected at the language level. When you pass an array name to a function, it decays to a pointer to its first element, which means the function can modify the original array directly. The pointers and arrays in C article covers that relationship in depth and is the natural next step after this one. For a full set of question patterns across C data structures, loops, and strings, the 31 most-asked C programming interview questions article maps the formats used in standard placement screening tests.
Limitations Worth Knowing Before an Interview
Arrays are fast and simple, but three constraints matter in practice:
- Fixed size. In standard C (pre-C99), the size must be a compile-time constant. C99 added variable-length arrays, but C11 made them optional. If you underestimate, you run out of space. If you overestimate, memory is wasted.
- Insertion and deletion cost O(n). Adding or removing an element anywhere except the end requires shifting every element after it. For frequent insertions and deletions in the middle, a linked list is a better fit.
- No bounds checking. C will not stop you from reading or writing past the end of an array. The result is undefined behaviour — sometimes a crash, sometimes silent data corruption, sometimes an apparent correct result that hides a bug until production. This is why bounds-conscious loop conditions are a standard interview checkpoint.
The traversal and sorting operations you have practiced in this article are not just exam material. Those same primitives, applied to token sequences instead of integer arrays, are how language models process text at the API level. TinkerLLM is where you can run actual LLM API calls; ₹299 gives you a working key and enough sandbox time to build a small text-processing project that is more interesting to show a recruiter than another sorted integer array.
Primary sources
Frequently asked questions
Does array index in C start from 0 or 1?
Array indices in C start from 0. An array declared as int arr[5] has valid indices 0 through 4. Accessing index 5 is out-of-bounds and produces undefined behaviour.
Can you change the size of an array after declaring it in C?
No. Arrays in C have a fixed size set at declaration. For a resizable collection, use dynamic memory allocation with malloc() and realloc(), or switch to a linked list.
What happens if you access an array element out of bounds in C?
Out-of-bounds access in C is undefined behaviour. The compiler will not warn you, and the program may crash, silently corrupt data, or appear to work correctly depending on what sits at that memory address.
What is the difference between a 1D and 2D array in C?
A 1D array stores a linear sequence of elements, for example int arr[5]. A 2D array stores elements in rows and columns, for example int matrix[3][4], which has 3 rows and 4 columns for a total of 12 elements.
How do you pass an array to a function in C?
Pass the array name and its size as separate parameters, for example void process(int arr[], int n). The array name decays to a pointer to its first element, so changes inside the function affect the original array.
Why does C not check array bounds automatically?
C prioritises runtime performance over safety. Bounds checking adds a comparison on every array access. Languages like Java and Python check bounds automatically; C trusts the programmer to track indices correctly.
A self-paced playground for building with LLMs.
TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.
Try TinkerLLM (₹299 launch)