Introduction
An array in C is a collection of elements of the same data type stored in contiguous memory locations. Arrays allow programmers to store multiple values under a single name and access them using an index.
Key Points
1. Array Declaration
- Syntax:
dataType arrayName[size]; - Example:
int numbers[5]; // Array of 5 integers
2. Array Initialization
- Assign values during declaration:
int numbers[5] = {10, 20, 30, 40, 50}; - Partial initialization: Remaining elements default to 0
3. Accessing Array Elements
- Use indices starting from 0
- Example:
printf("%d", numbers[2]); // Outputs 30
4. Types of Arrays
- Single-dimensional array (1D) – Linear list of elements
- Multi-dimensional array (2D, 3D) – Matrix-like data storage
5. Advantages
- Stores multiple values in a single variable
- Easy access using indices
- Efficient for iterative processing
6. Limitations
- Fixed size (static arrays)
- Cannot store mixed data types
Example Code
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
int i;
for(i = 0; i < 5; i++) {
printf("numbers[%d] = %d\n", i, numbers[i]);
}
return 0;
}