5 Arrays in C

Arrays in C Language
An array in C stores multiple values of the same data type under one name.
 
1️ Definition of an Array
An array is a collection of elements of the same data type stored in continuous memory locations.
Syntax:
data_type array_name[size];
Example:
int marks[5];
This creates an array named marks that can store 5 integers.
 
2️ Initialization of Arrays
Method 1: During Declaration
int marks[5] = {85, 90, 78, 88, 92};
Method 2: Without Specifying Size
int marks[] = {85, 90, 78, 88, 92};
Partial Initialization
int marks[5] = {85, 90};
Remaining elements become 0.
 
3️ Accessing Array Elements
Each element is accessed using an index number. Index starts from 0, not 1.
printf(“%d”, marks[0]);   // Prints first element
If marks = {85, 90, 78, 88, 92}

Index Value
0 85
1 90
2 78
3 88
4 92

 
4️ Reading Values into an Array
int i, marks[5];
for(i = 0; i < 5; i++)
{
scanf(“%d”, &marks[i]);
}
 
5️ Printing Array Elements
for(i = 0; i < 5; i++)
{
printf(“%d “, marks[i]);
}
 
6️ Types of Arrays
(a) One-Dimensional Array
A simple list.
int numbers[5];
 
(b) Two-Dimensional Array
Like a table with rows and columns.
int matrix[3][3];
Initialization:
int matrix[2][2] = {
{1, 2},
{3, 4}
};

Accessing:
printf(“%d”, matrix[0][1]);   // Prints 2
 
7️ Important Points About Arrays
Stores same data type only
Index starts from 0
Memory is allocated continuously
Size must be constant (in basic C programs)
Out-of-bound access leads to garbage values or errors
 
8 Advantages of Arrays
Easy to store large data
Efficient memory usage
Useful in sorting and searching
Forms base for advanced data structures
 
 
(i) Passing an Entire Array as an Argument to a Function
When an array is passed to a function, the name of the array is given as the argument. The function receives the array and can access all its elements.
Example:
#include <stdio.h>
void display(int a[], int n)
{
int i;
for(i = 0; i < n; i++)
{
printf(“%d “, a[i]);
}
}
int main()
{
int arr[5] = {10, 20, 30, 40, 50};
display(arr, 5);   // passing the whole array
return 0;
}
Explanation:

  • arr is the array passed as argument.
  • The function display() receives the array as a[] and prints all elements.

 
(ii) Passing Individual Elements of an Array as Arguments
Instead of passing the entire array, individual elements of the array can be passed to a function.
Example:
#include <stdio.h>
void printElement(int x)
{
printf(“%d “, x);
}
int main()
{
int arr[5] = {10, 20, 30, 40, 50};
int i;
for(i = 0; i < 5; i++)
{
printElement(arr[i]);   // passing individual elements
}
return 0;
}
Explanation:

  • Each element like arr[i] is passed one by one to the function printElement().
  • The function prints the value received in parameter x.

 
Summary

Method How it is Passed Example
Entire Array Array name is passed display(arr, 5);
Individual Elements Single element is passed printElement(arr[i]);

Top of Form
Bottom of Form
 
 
9 Example Program: Find Sum of Array Elements
#include <stdio.h>
int main()
{
int i, sum = 0;
int arr[5] = {1, 2, 3, 4, 5};
for(i = 0; i < 5; i++)
{
sum = sum + arr[i];
}
printf(“Sum = %d”, sum);
return 0;
}

Output:
Sum = 15
 
 
 
 
 
#include <stdio.h>
#include <string.h>
 
int main()
{
char name[10][30];
char temp[30];
int i, j, min;
 
// Input 10 names
printf(“Enter 10 names:\n”);
for(i = 0; i < 10; i++)
{
gets(name[i]);
}
 
// Selection Sort
for(i = 0; i < 9; i++)
{
min = i;
for(j = i + 1; j < 10; j++)
{
if(strcmp(name[j], name[min]) < 0)
{
min = j;
}
}
 
// Swapping
if(min != i)
{
strcpy(temp, name[i]);
strcpy(name[i], name[min]);
strcpy(name[min], temp);
}
}
 
// Display sorted names
printf(“\nNames in Alphabetical Order:\n”);
for(i = 0; i < 10; i++)
{
puts(name[i]);
}
 
return 0;
}