Arrays in C for Absolute Beginners
Introduction
In C programming, an array is a way to store multiple values in a single variable. Imagine you want to keep track of the marks for five subjects. Instead of creating five separate variables, you can use an array to store all these marks in one place. This post introduces arrays in C and guides you through four simple examples.
What is an Array?
An array in C is a collection of variables of the same type, stored together in a continuous block of memory. Each element in an array can be accessed by its index (position in the array). In C, array indexes start from 0, so the first element is at index 0, the second at index 1, and so on.
Declaring an Array
To declare an array, we specify the data type, the array name, and the size of the array. Here’s a basic syntax:
data_type array_name[array_size];
For example:
int marks[5];
This line declares an array called marks
that can hold 5 integers.
Example 1: Declaring and Initializing an Array
In this first example, we’ll create an array, initialize it with values, and print each value.
Code
#include <stdio.h>
int main() {
int marks[5] = {80, 85, 90, 75, 88}; // Initializing the array with values
printf("Marks in each subject:\n");
for(int i = 0; i < 5; i++) {
printf("Subject %d: %d\n", i+1, marks[i]);
}
return 0;
}
Explanation
- We declare an array
marks
with 5 elements. - Each element is assigned a value representing marks.
- A
for
loop is used to print each mark.
Output
Marks in each subject:
Subject 1: 80
Subject 2: 85
Subject 3: 90
Subject 4: 75
Subject 5: 88
Example 2: Taking User Input in an Array
In this example, we’ll ask the user to enter values into the array, then print them back.
Code
#include <stdio.h>
int main() {
int marks[5];
// Taking input from the user
printf("Enter marks for 5 subjects:\n");
for(int i = 0; i < 5; i++) {
printf("Subject %d: ", i+1);
scanf("%d", &marks[i]);
}
// Printing the entered marks
printf("\nYou entered:\n");
for(int i = 0; i < 5; i++) {
printf("Subject %d: %d\n", i+1, marks[i]);
}
return 0;
}
Explanation
- We declare an array
marks
with 5 elements. - Using a loop, we ask the user to input marks for each subject.
- Finally, we print the marks that the user entered.
Example Output
Enter marks for 5 subjects:
Subject 1: 80
Subject 2: 85
Subject 3: 90
Subject 4: 75
Subject 5: 88
You entered:
Subject 1: 80
Subject 2: 85
Subject 3: 90
Subject 4: 75
Subject 5: 88
Example 3: Finding the Sum of Elements in an Array
Here, we’ll use an array to store marks and then calculate the total marks by adding all the elements in the array.
Code
#include <stdio.h>
int main() {
int marks[5];
int sum = 0;
// Taking input from the user
printf("Enter marks for 5 subjects:\n");
for(int i = 0; i < 5; i++) {
printf("Subject %d: ", i+1);
scanf("%d", &marks[i]);
}
// Calculating the sum of elements
for(int i = 0; i < 5; i++) {
sum += marks[i];
}
printf("Total Marks: %d\n", sum);
return 0;
}
Explanation
- We declare an integer array
marks
and a variablesum
to store the total. - We prompt the user to enter marks for each subject.
- Using a loop, we add each mark to
sum
. - Finally, we print the total marks.
Example Output
Enter marks for 5 subjects:
Subject 1: 80
Subject 2: 85
Subject 3: 90
Subject 4: 75
Subject 5: 88
Total Marks: 418
Example 4: Finding the Maximum Element in an Array
In this example, we’ll find the highest mark in the array using a loop.
Code
#include <stdio.h>
int main() {
int marks[5];
int max;
// Taking input from the user
printf("Enter marks for 5 subjects:\n");
for(int i = 0; i < 5; i++) {
printf("Subject %d: ", i+1);
scanf("%d", &marks[i]);
}
// Assuming the first element is the maximum initially
max = marks[0];
// Finding the maximum element
for(int i = 1; i < 5; i++) {
if(marks[i] > max) {
max = marks[i];
}
}
printf("Highest Marks: %d\n", max);
return 0;
}
Explanation
- We declare an array
marks
and a variablemax
to store the highest mark. - After taking input from the user, we assume the first mark is the highest.
- We use a loop to compare each mark to
max
. If a mark is higher, we updatemax
. - Finally, we print the highest mark.
Example Output
Enter marks for 5 subjects:
Subject 1: 80
Subject 2: 85
Subject 3: 90
Subject 4: 75
Subject 5: 88
Highest Marks: 90
Conclusion
Arrays make it easy to handle collections of data in C. You can store multiple values in a single variable, access them with indexes, and perform operations like summing and finding maximum values. Practice using arrays by experimenting with these examples—try increasing the array size or adding more functionalities.
Happy Coding!