bing
Flat 10% & upto 50% off + Free additional Courses. Hurry up!

C Array

 

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

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

 

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.

arrayInitialization of C Array

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

 

Two Dimensional Array (2D Array)

2D array is represented in the form of rows and columns which is known as matrix. Youcan 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;

"0 Responses on C Array"

Training in Cities

Bangalore, Hyderabad, Chennai, Delhi, Kolkata, UK, London, Chicago, San Francisco, Dallas, Washington, New York, Orlando, Boston

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.

top

Sales Offer

  • To avail this offer, enroll before 05th December 2016.
  • This offer cannot be combined with any other offer.
  • This offer is valid on selected courses only.
  • Please use coupon codes mentioned below to avail the offer
offer-june

Sign Up or Login to view the Free C Array.