Arrays in C
Imagine you are developing KauraX and need to store the marks of 100 students.
Without arrays, you would have to declare 100 different variables:
cint marks1; int marks2; int marks3; ... int marks100;
Managing so many variables is difficult and inefficient.
To solve this problem, C provides arrays. An array allows you to store multiple values of the same data type using a single variable name.
Arrays are one of the most fundamental data structures in programming and are widely used in real-world software development.
🎯 Learning Objectives
After completing this lesson, you will be able to:
- Understand what arrays are.
- Declare and initialize arrays.
- Access and modify array elements.
- Traverse arrays using loops.
- Perform common array operations.
- Work with two-dimensional arrays.
- Understand common mistakes and best practices.
📖 What is an Array?
An array is a collection of elements of the same data type stored in contiguous memory locations.
Each element is accessed using its index.
Example:
cint marks[5];
This creates an array capable of storing five integers.
Visual Representation
textmarks +----+----+----+----+----+ | 75 | 80 | 65 | 90 | 85 | +----+----+----+----+----+ 0 1 2 3 4
Notice that indexing starts from 0, not 1.
Why Use Arrays?
Arrays help us:
- Store multiple values using one variable.
- Reduce code repetition.
- Process large datasets efficiently.
- Simplify searching and sorting.
Array Declaration
Syntax
cdatatype arrayName[size];
Example
cint marks[5];
Array Initialization
Method 1
cint marks[5] = {75,80,65,90,85};
Method 2
cint marks[] = {10,20,30,40};
The compiler automatically determines the size.
Accessing Array Elements
Each element is accessed using its index.
Example
c#include <stdio.h> int main() { int marks[5] = {75,80,65,90,85}; printf("%d", marks[2]); return 0; }
Output
text65
Modifying Array Elements
cmarks[1] = 95;
Now the array becomes:
text75 95 65 90 85
Reading Array Values
c#include <stdio.h> int main() { int marks[5]; int i; printf("Enter 5 Marks:\n"); for(i=0;i<5;i++) { scanf("%d",&marks[i]); } return 0; }
Printing Array Elements
c#include <stdio.h> int main() { int marks[5]={75,80,65,90,85}; int i; for(i=0;i<5;i++) { printf("%d ",marks[i]); } return 0; }
Output
text75 80 65 90 85
Finding Sum of Elements
c#include <stdio.h> int main() { int arr[5]={10,20,30,40,50}; int i,sum=0; for(i=0;i<5;i++) { sum += arr[i]; } printf("Sum = %d",sum); return 0; }
Output
textSum = 150
Finding Average
c#include <stdio.h> int main() { int marks[5]={80,75,90,85,70}; int i,sum=0; float average; for(i=0;i<5;i++) { sum += marks[i]; } average=(float)sum/5; printf("Average = %.2f",average); return 0; }
Output
textAverage = 80.00
Finding Largest Element
c#include <stdio.h> int main() { int arr[5]={15,8,42,30,20}; int i,max; max=arr[0]; for(i=1;i<5;i++) { if(arr[i]>max) max=arr[i]; } printf("Largest = %d",max); return 0; }
Output
textLargest = 42
Finding Smallest Element
c#include <stdio.h> int main() { int arr[5]={15,8,42,30,20}; int i,min; min=arr[0]; for(i=1;i<5;i++) { if(arr[i]<min) min=arr[i]; } printf("Smallest = %d",min); return 0; }
Searching an Element
c#include <stdio.h> int main() { int arr[5]={10,20,30,40,50}; int i,key; printf("Enter number: "); scanf("%d",&key); for(i=0;i<5;i++) { if(arr[i]==key) { printf("Found at index %d",i); return 0; } } printf("Not Found"); return 0; }
Two-Dimensional Arrays
A two-dimensional array represents data in rows and columns.
Syntax
cdatatype array[row][column];
Example
cint matrix[3][3];
Visual Representation
textCol0 Col1 Col2 Row0 1 2 3 Row1 4 5 6 Row2 7 8 9
Initializing a 2D Array
cint matrix[2][3]= { {1,2,3}, {4,5,6} };
Printing a Matrix
c#include <stdio.h> int main() { int matrix[2][3]= { {1,2,3}, {4,5,6} }; int i,j; for(i=0;i<2;i++) { for(j=0;j<3;j++) { printf("%d ",matrix[i][j]); } printf("\n"); } return 0; }
Output
text1 2 3 4 5 6
Real-World Example
c#include <stdio.h> int main() { int progress[5]={80,90,75,60,95}; int i; printf("KauraX Student Progress\n\n"); for(i=0;i<5;i++) { printf("Student %d : %d%%\n",i+1,progress[i]); } return 0; }
Common Beginner Mistakes
Accessing Invalid Index
Wrong
cmarks[5]
A five-element array has valid indexes:
text0 1 2 3 4
Using Wrong Loop Condition
Wrong
cfor(i=0;i<=5;i++)
Correct
cfor(i=0;i<5;i++)
Uninitialized Arrays
Wrong
cint arr[5]; printf("%d",arr[0]);
Always initialize arrays before use.
Accessing an index outside the array bounds leads to undefined behavior, which can cause crashes or incorrect results.
Best Practices
- Use meaningful array names.
- Avoid hardcoding array sizes repeatedly.
- Validate user input.
- Always stay within array bounds.
- Use loops to process arrays.
🧠 Interview Questions
- What is an array?
- Why do array indexes start from zero?
- Differentiate between one-dimensional and two-dimensional arrays.
- How are arrays stored in memory?
- What happens if an invalid index is accessed?
- How do you find the largest element in an array?
- Explain array traversal.
✍ Practice Questions
- Read and print 10 numbers.
- Find the sum of array elements.
- Calculate the average of marks.
- Find the largest and smallest element.
- Search for an element in an array.
- Reverse an array.
- Count even and odd numbers.
- Merge two arrays.
- Print a matrix.
- Find the transpose of a matrix.
📚 Lesson Summary
In this lesson, you learned:
- What arrays are.
- Array declaration and initialization.
- Accessing and modifying elements.
- Traversing arrays using loops.
- Finding sum, average, largest, and smallest values.
- Searching elements.
- Two-dimensional arrays.
- Common mistakes and best practices.
Arrays provide an efficient way to store and process multiple values using a single variable name. They are the foundation for many advanced data structures and algorithms.
🚀 What's Next?
In the next lesson, Strings in C, you'll learn:
- Character arrays
- String declaration
- Input and output
- Standard string functions
strlen()strcpy()strcmp()strcat()- Practical programs
- Interview questions
Strings are simply arrays of characters and are essential for working with text in C.
Happy Coding with KauraX 💙