0% found this document useful (0 votes)
5 views17 pages

Arrays

Uploaded by

tejnakhosla
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
5 views17 pages

Arrays

Uploaded by

tejnakhosla
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 17

Storage Classes

Every variable in C possesses a characteristic called its


storage class. The storage class defines two
characteristics of the variable:
Lifetime: refers to the length of time it retain a
particular value.
Visibility/Scope: refers to accessibility of a variable.

A variable’s storage class tells us:


 What is the scope of variable.
 What is the life of variable.
 Where the variable would be stored.
 What will be the initial value of the variable.
There are four storage classes in C:
 Automatic
 Register
 Static
 Extern
Storage Storage Initial Visibility Lifetime
Class Value (Declaration valid (Value alive till)
in)
Auto Memory Garbage Local to the Value is
block/function retained within
the block

Register Register Garbage Local to the Value is


block/function retained within
the block

Static Memory Zero Local to the Value is


block/function retained among
all the functions

Extern Memory Zero To all the functions Till program


termination
Arrays
An array is a collection of homogeneous data types referred
under same name.
Eg :
int salary[500];

We can refer to the individual salaries by writing a number


called as index/subscript.
Individual values are called elements while complete set of
values is referred to as array.
types
 1 Dimensional
One subscript is required to refer to an element.
Syntax:
Datatype arrayname[size];

 2 Dimensional
Two subscripts are required to refer to an element.
Syntax:
Datatype arrayname[no of rows][no of columns];
Inputting the elements
I D Array 2 D Array

int i, a[10]; int i,j,b[2][3];


for(i=0;i<=9;i++) for(i=0;i<2;i++)
scanf(“%d”,&a[i]); {
for(j=0;j<3;j++)
{
scanf(“%d”,&b[i][j]);
}
}
outputting the elements
int i, a[10]; int i,j,b[2][3];
for(i=0;i<=9;i++) for(i=0;i<2;i++)
printf(“%d”,a[i]); {
for(j=0;j<3;j++)
{
printf(“%d”,b[i]
[j]);
}
}

I D Array 2 D Array
2-D Array
Definition
Many applications require that data be organized in more
than one dimension. One common example is a matrix (a
table), which is an array that consists of rows and columns.

Syntax:
type arrayname[size of dimension 1][size of dimension 2];
Dimention 1 denotes rows
Dimention 2 denotes columns
E.g.
int table [3][3];
denotes a 2 dimensional array with 3 rows and 3 columns.
Declaration
Like one dimensional array, declaration of 2D array tells the
compiler the name of the array, the type of its elements, and
the size of each dimension.
E.g.1
Int table[4][4]={0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3};

E.g. 2
int table[4][4]={
{0,0,0,0},
{1,1,1,1},
{2,2,2,2},
{3,3,3,3}
};
E.g.3
int table[][3]= {
{0,0,0},
{1,1,1}
};
E.g.4
int table[2][3]={
{1,1},
{2}
};
this will initialize first two elements of row one to 1, first element
of row two to 2 and rest of the elements as 0.
E.g.5
Int arr[3][5]={ {0}, {0}, {0} };
This will initialize all the elements to zero.
Inputting values

int table[3][4];

for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
scanf(“%d”,&table[i][j]);
}
}
Outputting values

for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf(“%d”,table[i][j]);
}
}
Multidimensional array
C allows arrays of three or more dimensions.

Syntax:
Type nameof array[d1][d2][d3]….[dn];

E.g.
int table [3][5][4];
example
int table[2][2][2]={
{
{1,1}
{1,1}
},
{
{2,2}
{2,2}
}
};
Bit wise operators

You might also like