• Articles
  • Tutorials
  • Interview Questions

C Arrays(With Examples and Syntax)

What are Arrays in C?

Array is a collection of elements which are of similar types. Array is very useful in C.
Suppose we want to store 50 students marks then for this purpose we need to use 50 variable which is not possible and hard to manage so to avoid this situation we use array in which 50 students marks can store in only one variable easily.

Advantages of Array in C

  • Code Optimization
  • Easy to traverse data
  • Easy to sort data
  • Random Access

Watch this C Programming & Data Structure by Intellipaat:

Video Thumbnail

You can declare an array as follows:

data_type array_name[array_size];

e.g.

int a[5];

where int is data type of array a which can store 5 variables.
array

Initialization of Array in C

You can initialize array by using index. Always array index starts from 0 and ends with [array_size – 1].

a[0] = 20;
a[1] = 40;

Or you can also declare array with initialization like as follows:-

int a[3] = {20,30,40};

Example

#include <stdio.h>
#include <conio.h>
void main()
{
int a[3], i;   //declaration of array
a[0] = 20; //initialization of array
a[1] = 30;
a[2] = 40;
for(i=0;i<3;i++)
{
printf(“%d\n”,a[i]);
}
getch();
}

Output

20
30
40

Certification in Full Stack Web Development

Two Dimensional Array (2D Array)

2D array is represented in the form of rows and columns which is known as matrix. You can declare an 2Darray as follows:

data_type array_name[size1][size2];

Where size1 represents the number of rows and size2 represents the number of columns.
e.g.

int a[2][3];

Initialization of 2D Array 

A way to initialize the two dimensional array at the time of declaration is as follows:-

int arr[2][3]={{2,3,3},{2,3,4}};

Example

#include <stdio.h>
#include <conio.h>
void main()
{
int a[2][2] = {{1,3},{2,4}};
for(i=0;i<2;i++)
{
for(j=0;j<2;j++){
printf("a[%d] [%d] = %d \n",i,j,a[i][j]);
}//end of j
}//end of i
getch();
}

Output

a[0][0] = 1;
a[0][1] = 3;
a[1][0] = 2;
a[1][1] = 4;

Course Schedule

Name Date Details
Python Course 14 Dec 2024(Sat-Sun) Weekend Batch View Details
21 Dec 2024(Sat-Sun) Weekend Batch
28 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration.