C Program to Demonstrate Arrays with Example
Learn C step by step.
Explore blog under C language - Arrays
Jan 22, 2026
## Chapter 7: Arrays
When we need to store multiple values ββof the same type, creating separate variables is not the right approach. In such situations, we use arrays.
An array = a collection of elements of the same data type. These elements are stored in contiguous memory locations.
Why are arrays necessary?
Suppose you need to store the marks of 5 students:
Wrong way:
```c
int m1, m2, m3, m4, m5;
```
Correct way:
```c
int marks[5];
```
This results in:
* Shorter code
* Easier data management
* Faster processing with loops
One-Dimensional Array
In a one-dimensional array, data is stored in a straight line.
Syntax (Declaration)
```c
data_type array_name[size];
```
Declaration and Initialization:
// Method 1: Declare and initialize
```c
#include
int main() {
// Array with 5 elements
int values[5] = {5, 10, 15, 20, 25};
printf("First element: %dn", values[0]);
printf("Last element: %dn", values[4]);
return 0;
}
```
Here:
* values[0] is the first element
* Array index always starts from 0
// Method 2: Partial Initialization
```c
#include
int main() {
// Only 2 values ββare given
int marks[5] = {70, 80};
// The remaining elements automatically become 0
for(int i = 0; i < 5; i++) {
printf("marks[%d] = %dn", i, marks[i]);
}
return 0;
}
```
In the output:
* marks[0] = 70
* marks[1] = 80
* All others are 0
// Method 3: Size is determined by the initializer
```c
#include
int main() {
// size will be decided automatically
int years[] = {2000, 2005, 2010, 2015};
int length = sizeof(years) / sizeof(years[0]);
printf("Array size: %dn", length);
return 0;
}
```
This method is very useful when we don't know the size beforehand.
// Accessing elements
In C language, every element of an array has an index number.
Very important thing to remember:
Example: Element Access and Modification
```c
#include
int main() {
int data[4] = {12, 18, 24, 30};
// Accessing elements
printf("First value: %dn", data[0]); // index 0
printf("Third value: %dn", data[2]); // index 2
// Changing the value of an element
data[1] = 20; // Previously 18, now 20
printf("Modified second value: %dn", data[1]);
return 0;
}
```
What's happening here?
* data[0] β First element
* data[2] β Third element
* data[1] = 20 β Changed the value of the second element
Array elements can be both read and modified.
Important: Array indices start from 0, not 1!
Looping through arrays:
When an array has many elements, accessing each element manually is not practical. Therefore, we use loops.
Practical Example: Marks, Sum, and Average
```c
#include
int main() {
int scores[5] = {70, 82, 90, 76, 88};
int total = 0;
printf("Student Marks:n");
// Printing all marks using a loop
for(int index = 0; index < 5; index++) {
printf("Student %d's marks: %dn", index + 1, scores[index]);
total += scores[index]; // Calculating the sum
}
// Calculating the average
float avg = (float)total / 5;
printf("nAverage marks: %.2fn", avg);
return 0;
}
```
Step-by-step explanation:
1. The `scores` array stores the marks of 5 students.
2. The `for` loop accesses each element one by one.
3. All the marks are added to `total`.
4. Typecasting is used to calculate the average.
What do we learn from this example?
* An array is practically useless without a loop.
* The `< size` condition is very important.
* Typecasting is necessary when calculating the average.
* Real-life problems can be easily solved using arrays and loops.
Finding the Maximum Element:
How to find the maximum element in an array? Sometimes we need to know which is the largest value (maximum element) in an array. We can easily solve this problem using a loop and comparison.
Logic (In Simple Language)
1. First, assume the first element of the array is the maximum.
2. Then, use a loop to compare each element.
3. If any element is greater than the current maximum, β update the maximum.
4. After the loop finishes, we will have the largest value.
Practical Example: Maximum Element Program (Original Code)
```c
#include
int main() {
int data[] = {32, 76, 15, 98, 54, 41};
int length = 6;
// Assume the first element is the largest
int largest = data[0];
// Check the remaining elements using a loop
for(int pos = 1; pos < length; pos++) {
if(data[pos] > largest) {
largest = data[pos]; // Found a new maximum
}
}
printf("Largest value in the array: %dn", largest);
return 0;
}
```
Two-Dimensional Arrays
When data needs to be stored in the form of rows and columns, we use a two-dimensional array. You can think of it as a table or a matrix.
Examples:
* Marks table of Students Γ Subjects
* Matrix in mathematics
* Grid-based data
What is a 2D Array?
* It has rows and columns.
* Two indices are required to access each element:
* First β row
* Second β column
Syntax (Declaration)
```c
data_type array_name[rows][columns];
```
Practical Example: 2D Array (Matrix)
```c
#include
int main() {
// Matrix with 2 rows and 3 columns
int table[2][3] = {
{10, 20, 30},
{40, 50, 60}
};
printf("Elements of the matrix:n");
// Outer loop for rows
for(int r = 0; r < 2; r++) {
// Inner loop for columns
for(int c = 0; c < 3; c++) {
printf("%4d", table[r][c]);
}
printf("n"); // New line
}
return 0;
}
```
Understanding the code in simple terms:
1. Array Declaration: `int table[2][3];`
* 2 rows
* 3 columns
* Total elements = 2 Γ 3 = 6
2. Why is a Nested Loop necessary?
* The outer loop changes the rows.
* The inner loop changes the columns.
Therefore, a nested loop (a loop inside a loop) is necessary to print/process a 2D array.
3. Element Access
* `table[0][0]` // First row, first column
* `table[1][2]` // Second row, third column
Output
```
Elements of the matrix:
10 20 30
40 50 60
```
---
## Conclusion
In this article, we explored the core concepts of Explore blog under C language - Arrays. Understanding these fundamentals is crucial for any developer looking to master this topic.
## Frequently Asked Questions (FAQs)
**Q: What is Explore blog under C language - Arrays?**
A: Explore blog under C language - Arrays is a fundamental concept in this programming language/topic that allows developers to perform specific tasks efficiently.
**Q: Why is Explore blog under C language - Arrays important?**
A: It helps in organizing code, improving performance, and implementing complex logic in a structured way.
**Q: How to get started with Explore blog under C language - Arrays?**
A: You can start by practicing the basic syntax and examples provided in this tutorial.
**Q: Are there any prerequisites for Explore blog under C language - Arrays?**
A: Basic knowledge of programming logic and syntax is recommended.
**Q: Can Explore blog under C language - Arrays be used in real-world projects?**
A: Yes, it is widely used in enterprise-level applications and software development.
**Q: Where can I find more examples of Explore blog under C language - Arrays?**
A: You can check our blog section for more advanced tutorials and use cases.
**Q: Is Explore blog under C language - Arrays suitable for beginners?**
A: Yes, our guide is designed to be beginner-friendly with clear explanations.
**Q: How does Explore blog under C language - Arrays improve code quality?**
A: By providing a standardized way to handle logic, it makes code more readable and maintainable.
**Q: What are common mistakes when using Explore blog under C language - Arrays?**
A: Common mistakes include incorrect syntax usage and not following best practices, which we've covered here.
**Q: Does this tutorial cover advanced Explore blog under C language - Arrays?**
A: This article covers the essentials; stay tuned for our advanced series on this topic.