Arrays in C Programming for Beginners with Examples
Arrays in C Programming
An array in C programming is used to store multiple values of the same data type in a single variable. Instead of creating many separate variables, arrays help organize data efficiently.
What is an Array?
An array is a collection of elements stored in continuous memory locations. Each element is accessed using an index number.
Index in C starts from 0.
Declaration of an Array
Syntax:
data_type array_name[size];
Example:
int numbers[5];This declares an integer array that can store 5 values.
Initialization of an Array
int numbers[5] = {10, 20, 30, 40, 50};
Here:
- numbers[0] = 10
- numbers[1] = 20
- numbers[2] = 30
- numbers[3] = 40
- numbers[4] = 50
Accessing Array Elements
#include <stdio.h>
int main() {
int numbers[3] = {5, 10, 15};
printf("%d\n", numbers[0]);
printf("%d\n", numbers[1]);
printf("%d\n", numbers[2]);
return 0;
}
This program prints all array elements.
Why Arrays Are Important
- Store multiple values efficiently
- Reduce number of variables
- Used in sorting and searching programs
- Essential for advanced data structures
Conclusion
Arrays help manage multiple values using a single variable. Understanding arrays is important before learning strings and functions in C.
Comments
Post a Comment