-
Hajipur, Bihar, 844101
In C programming, an array is a collection of variables of the same data type, stored contiguously in memory under a single name. Arrays provide a way to store and manage multiple values efficiently, rather than creating separate variables for each piece of data. This is especially useful in programs that handle large sets of numbers, lists, or repeated inputs.
By using arrays, you can perform operations like calculations, sorting, and searching on multiple values using loops, making programs cleaner, faster, and more maintainable.
To use an array, you must first declare it, specifying the data type, the array name, and its size:
data_type array_name[size];
data_type: The type of elements, such as int, float, or char.
array_name: A name to reference the collection of values.
size: The total number of elements the array can hold (must be a positive integer).
Example:
int numbers[5]; // An array of 5 integers
Here, numbers can hold five integers, and its indices range from 0 to 4. Arrays in C are zero-indexed, meaning the first element is always at index 0.
Arrays can be initialized at the time of declaration, giving them initial values:
int numbers[5] = {10, 20, 30, 40, 50};
If fewer values are provided than the declared size, the remaining elements are automatically set to 0:
int numbers[5] = {1, 2}; // {1, 2, 0, 0, 0}
You can also allow the compiler to determine the size automatically:
int numbers[] = {5, 10, 15, 20}; // size is 4
Initializing arrays ensures predictable behavior and prevents undefined or garbage values.
Array elements are accessed using the index number:
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
printf("First element: %d\n", numbers[0]);
printf("Third element: %d\n", numbers[2]);
return 0;
}
Output:
First element: 10
Third element: 30
Always remember that accessing an index outside the array’s bounds leads to undefined behavior, which can cause errors or crashes.
Arrays allow direct access to each element without needing multiple variable names, which makes code clean and easy to maintain.
You can update an element by assigning a new value to its index:
numbers[1] = 25; // Change second element
printf("%d\n", numbers[1]); // 25
Arrays are mutable, meaning values can be modified at any point in the program, providing flexibility for operations like updating scores, changing states, or adjusting measurements.
Arrays are most powerful when combined with loops, allowing you to process all elements efficiently:
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
int sum = 0;
for(int i = 0; i < 5; i++) {
sum += numbers[i]; // Add each element
}
printf("Sum of array elements: %d\n", sum);
return 0;
}
Using loops avoids repetitive code and enables dynamic calculations.
Loops make it possible to perform operations like finding maximum, minimum, or average values efficiently.
C allows two-dimensional arrays, which can be imagined as tables or matrices:
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
Access elements using two indices: matrix[0][0] is 1, matrix[1][2] is 6.
Nested loops are typically used to traverse these arrays:
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
Output:
1 2 3
4 5 6
Multidimensional arrays are essential in mathematics, games, and graphical applications.
Arrays hold elements of the same type.
Indexing starts at 0, not 1.
Array size must usually be known at compile time.
Arrays work efficiently with loops for processing multiple values.
Multidimensional arrays help handle complex data structures like grids and tables.
Arrays are foundational for advanced programming concepts, such as strings, pointers, and dynamic memory.
Accessing out-of-bounds indices.
Forgetting that the first index is 0.
Using uninitialized arrays, which contain garbage values.
Assuming arrays can store different data types; all elements must be uniform.
Not using loops to handle array operations, which can lead to repetitive code.
#include <stdio.h>
int main() {
int scores[5];
int sum = 0;
printf("Enter 5 scores:\n");
for(int i = 0; i < 5; i++) {
scanf("%d", &scores[i]);
sum += scores[i];
}
float average = sum / 5.0;
printf("Average score: %.2f\n", average);
return 0;
}
The program stores input in an array, calculates the sum, and then the average.
Loops make operations efficient and readable, even for larger datasets.
Arrays in C are a powerful tool for organizing data. They allow you to store multiple values under a single variable, access elements directly, and use loops for processing efficiently. From simple lists to multidimensional grids, arrays form the backbone of many programs. Understanding arrays is essential for managing data, performing calculations, and building advanced programs like matrices, tables, and dynamic systems.
Write a program to store 10 numbers in an array and print them in reverse order.
Create a program to calculate the sum and average of 7 numbers stored in an array.
Write a program to find the largest and smallest numbers in an array of 8 integers.
Develop a program that reads 5 numbers from the user and prints only the even numbers.
Write a program to search for a specific number in an array and print its index if found.
Create a program to copy one array into another of the same size.
Write a program to count the number of positive and negative numbers in an array of 10 integers.
Develop a program to reverse the elements of an array without using another array.
Write a program to add two arrays element-wise and store the result in a third array.
Create a program to print a 3x3 matrix entered by the user using a two-dimensional array.