C Notes
Learn what arrays are in C, why we need them, how to declare and initialize arrays, and understand memory layout with practical examples.
If you've ever needed to store multiple values of the same type — say, marks of 50 students or temperatures for each day of the month — you know how painful it would be to declare 50 separate variables. That's exactly the problem arrays solve in C programming.
An array is a collection of elements of the same data type, stored in contiguous memory locations. Instead of creating individual variables like marks1, marks2, marks3, you simply create one array called marks and access each element using an index.
Why Do We Need Arrays?
Consider this scenario: you want to store the marks of 5 students and calculate their average.
Without arrays:
int marks1 = 85, marks2 = 90, marks3 = 78, marks4 = 92, marks5 = 88;
float average = (marks1 + marks2 + marks3 + marks4 + marks5) / 5.0;Now imagine doing this for 500 students. It becomes impossible to manage. With arrays, the same task becomes elegant:
With arrays:
Key Characteristics of Arrays in C
| Feature | Description |
|---|---|
| Homogeneous | All elements must be of the same data type |
| Contiguous | Elements are stored in consecutive memory locations |
| Fixed Size | Size must be known at compile time (for static arrays) |
| Zero-Indexed | First element is at index 0 |
| Random Access | Any element can be accessed directly using its index |
How to Declare an Array in C
The syntax for declaring an array is straightforward:
Here are some examples:
When you declare an array like int numbers[10], the compiler allocates 10 × 4 = 40 bytes of contiguous memory (assuming int is 4 bytes on your system).
Array Initialization in C
There are several ways to initialize an array at the time of declaration.
Method 1: Initialize All Elements
Method 2: Partial Initialization
If you provide fewer values than the array size, remaining elements are automatically set to 0:
Method 3: Initialize All to Zero
Method 4: Size Determined by Initializer
You can omit the size and let the compiler count:
int marks[] = {85, 90, 78, 92, 88}; // Size is automatically 5Method 5: Designated Initializers (C99)
Memory Layout of Arrays
Understanding how arrays are stored in memory is crucial. Here's a visual representation:
| Array | int marks[5] = {85, 90, 78, 92, 88}; |
| Memory Address | 1000 1004 1008 1012 1016 |
| Value | | 85 | 90 | 78 | 92 | 88 | |
| Index | [0] [1] [2] [3] [4] |
Accessing Array Elements
You access array elements using the subscript operator [] with a zero-based index:
First element: 85 Third element: 78 Last element: 88 Modified second element: 95
Complete Example: Reading and Displaying Array Elements
Enter number of elements: 4 Enter 4 elements: 10 20 30 40 Array elements are: arr[0] = 10 arr[1] = 20 arr[2] = 30 arr[3] = 40
Common Mistakes with Arrays in C
- Array index out of bounds — C does not perform bounds checking. Accessing
marks[5]in a 5-element array leads to undefined behavior.
- Forgetting zero-indexing — The last valid index for
int arr[n]isn-1, notn.
- Using uninitialized arrays — Local arrays contain garbage values unless explicitly initialized.
- Assigning arrays directly — You cannot do
arr1 = arr2;. You must copy element by element.
Types of Arrays in C
| Type | Description | Example |
|---|---|---|
| One-Dimensional | Linear list of elements | int arr[10]; |
| Two-Dimensional | Matrix/Table format | int matrix[3][4]; |
| Multi-Dimensional | 3D and beyond | int cube[2][3][4]; |
Interview Questions
Q1: What happens if you access an array element beyond its declared size?
It results in undefined behavior. C does not perform runtime bounds checking, so you might read garbage values, crash the program, or corrupt other variables in memory.
Q2: Can the size of an array be changed after declaration in C?
No. Static arrays have a fixed size determined at compile time. If you need a resizable collection, you must use dynamic memory allocation with malloc() and realloc().
Q3: What is the difference between int arr[5] = {0} and int arr[5]?
int arr[5] = {0} initializes all elements to zero. int arr[5] (without initialization) leaves elements with garbage values if declared inside a function. Global/static arrays are automatically zero-initialized.
Q4: Why are arrays zero-indexed in C?
The index represents an offset from the base address. The first element is at offset 0 from the start of the array, hence index 0. The address formula base + (index × size) naturally starts at 0.
Summary
- Arrays store multiple values of the same type in contiguous memory
- Declaration syntax:
data_type name[size]; - Arrays are zero-indexed — first element is at index 0
- Uninitialized local arrays contain garbage values
- C does not check array bounds at runtime
- The array name represents the base address of the first element
- Arrays provide O(1) random access to any element using its index
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Introduction to Arrays in C Programming.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, arrays, introduction, introduction to arrays in c programming
Related C Programming Topics