0% found this document useful (0 votes)
37 views36 pages

C Notes-2

An array is a collection of similar data types stored in contiguous memory locations, with each element having a unique index. Arrays can be single-dimensional (vectors) or multi-dimensional (matrices), and they can be declared and initialized with specific syntax. The document also discusses accessing array elements, initializing arrays, and the use of functions with arrays, as well as string manipulation using character arrays.

Uploaded by

bsaiuttej100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views36 pages

C Notes-2

An array is a collection of similar data types stored in contiguous memory locations, with each element having a unique index. Arrays can be single-dimensional (vectors) or multi-dimensional (matrices), and they can be declared and initialized with specific syntax. The document also discusses accessing array elements, initializing arrays, and the use of functions with arrays, as well as string manipulation using character arrays.

Uploaded by

bsaiuttej100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

ARRAYS

Array is the collection of similar data types or collection of similar entity stored
in contiguous memory location. Array of character is a string. Each data item of
an array is called an element. And each element is unique and located in separated
memory location. Each of elements of an array share a variable but each element
having different index no. known as subscript.
An array can be a single dimensional or multi-dimensional and number of
subscripts determines its dimension. And number of subscript is always starts with
zero. One dimensional array is known as vector and two dimensional arrays are
known as matrix.
ADVANTAGES: array variable can store more than one value at a time
where other variable can store one value at a time.
Example:

int arr[100];
int mark[100];

DECLARATION OF AN ARRAY :

Its syntax is :

Data type array name [size];

int arr[100];

int mark[100];

int a[5]={10,20,30,100,5}

The declaration of an array tells the compiler that, the data type, name of the
array, size of the array and for each element it occupies memory space. Like for
int data type, it occupies 2 bytes for each element and for float it occupies 4 byte
for each element etc. The size of the array operates the number of elements that
can be stored in an array and it may be a int constant or constant int expression.

1
We can represent individual array as :

int ar[5];

ar[0], ar[1], ar[2], ar[3], ar[4];

Symbolic constant can also be used to specify the size of the array as:

#define SIZE 10;

INITIALIZATION OF AN ARRAY:

After declaration element of local array has garbage value. If it is global or


static array then it will be automatically initialize with zero. An explicitly it can
be initialize that
Data type array name [size] = {value1, value2, value3…}

Example:

in ar[5]={20,60,90, 100,120}
Array subscript always start from zero which is known as lower bound and upper
value is known as upper bound and the last subscript value is one less than the
size of array. Subscript can be an expression i.e. integer value. It can be any
integer, integer constant, integer variable, integer expression or return value from
functional call that yield integer value.
So if i & j are not variable then the valid subscript are
ar [i*7],ar[i*i],ar[i++],ar[3];
The array elements are standing in continuous memory locations and
the amount of storage required for hold the element depend in its size & type.
Total size in byte for 1D array is:

Total bytes=size of (data type) * size of array.

Example : if an array declared is:

int [20];

Total byte= 2 * 20 =40 byte.

2
ACCESSING OF ARRAY ELEMENT:

/*Write a program to input values into an array and display them*/

#include<stdio.h>
int main()
{
int arr[5],i;
for(i=0;i<5;i++)
{
printf(“enter a value for arr[%d] \n”,i);
scanf(“%d”,&arr[i]);
}

printf(“the array elements are: \n”);


for (i=0;i<5;i++)
{
printf(“%d\t”,arr[i]);
}
return 0;
}

OUTPUT:

Enter a value for arr[0] = 12

Enter a value for arr[1] =45

Enter a value for arr[2] =59

Enter a value for arr[3] =98

Enter a value for arr[4] =21

The array elements are 12 45 59 98 21

Example: From the above example value stored in an array are and occupy
its memory addresses 2000, 2002, 2004, 2006, 2008 respectively.

3
a[0]=12, a[1]=45, a[2]=59, a[3]=98, a[4]=21

ar[0] ar[1] ar[2] ar[3] ar[4]


12 45 59 98 21
2000 2002 2004 2006 2008

/* Write a program to add 10 array elements */

#include<stdio.h>

void main()

int i ;

int arr [10];

int sum=o;

for (i=0; i<=9; i++)

printf (“enter the %d element \n”, i+1);

scanf (“%d”, &arr[i]);

for (i=0; i<=9; i++)

sum = sum + a[i];

}
Example 2:
printf (“the sum of 10 array elements is %d”, sum);

OUTPUT:
4
Enter a value for arr[0] =5

Enter a value for arr[1] =10

Enter a value for arr[2] =15

Enter a value for arr[3] =20

Enter a value for arr[4] =25

Enter a value for arr[5] =30

Enter a value for arr[6] =35

Enter a value for arr[7] =40

Enter a value for arr[8] =45

Enter a value for arr[9] =50

Sum = 275

while initializing a single dimensional array, it is optional to specify the size of


array. If the size is omitted during initialization then the compiler assumes the size
of array equal to the number of initializers.
For example:-

int marks[]={99,78,50,45,67,89};

If during the initialization of the number the initializers is less then size of array,
then all the remaining elements of array are assigned value zero .
For example:-

int marks[5]={99,78};

Here the size of the array is 5 while there are only two initializers so After this
initialization, the value of the rest elements are automatically occupied by zeros
such as
Marks[0]=99 , Marks[1]=78 , Marks[2]=0, Marks[3]=0, Marks[4]=0

Again if we initialize an array like int array[100]={0};

5
Then the all the element of the array will be initialized to zero. If the number of
initializers is more than the size given in brackets then the compiler will show
an error.

For example:-

int arr[5]={1,2,3,4,5,6,7,8};//error

we cannot copy all the elements of an array to another array by simply assigning
it to the other array like, by initializing or declaring as
int a[5] ={1,2,3,4,5};

int b[5];

b=a;//not valid

(note:-here we will have to copy all the elements of array one by one, using for
loop.)

Single dimensional arrays and functions

/*program to pass array elements to a function*/

#include<stdio.h>

void main()

int arr[10],i;

printf(“enter the array elements\n”);

for(i=0;i<10;i++)

scanf(“%d”,&arr[i]);

check(arr[i]);

6
}
void check(int num)

if(num%2=0)

printf(”%d is even \n”,num);

else

printf(”%d is odd \n”,num);

Two dimensional arrays

Two dimensional array is known as matrix. The array declaration in both the array
i.e.in single dimensional array single subscript is used and in two dimensional
array two subscripts are is used.
Its syntax is

Data-type array name[row][column];

Or we can say 2-d array is a collection of 1-D array placed one below the other.

Total no. of elements in 2-D array is calculated as row*column

Example:-

int a[2][3];

Total no of elements=row*column is 2*3 =6

It means the matrix consist of 2 rows and 3 columns

7
For example:-

20 2 7

8 3 15

Positions of 2-D array elements in an array are as below

00 01 02

10 11 12

a [0][0] a [0][1] a [0][2] a [1][0] a [1][1] a [1][2]

20 2 7 8 3 15

2000 2002 2004 2006 2008

Accessing 2-d array /processing 2-d arrays


For processing 2-d array, we use two nested for loops. The outer for
loop corresponds to the row and the inner for loop corresponds to the
column.
For example

int a[4][5];

for reading value:-


for(i=0;i<4;i++)

for(j=0;j<5;j++)

scanf(“%d”,&a[i][j]);

}
8
}

For displaying value:-

for(i=0;i<4;i++)

for(j=0;j<5;j++)

printf(“%d”,a[i][j]);

Initialization of 2-d array:

2-D array can be initialized in a way similar to that of 1-D array. for example:-

int mat[4][3]={11,12,13,14,15,16,17,18,19,20,21,22};

These values are assigned to the elements row wise, so the values
of elements after this initialization are
Mat[0][0]=11, Mat[1][0]=14, Mat[2][0]=17 Mat[3][0]=20

Mat[0][1]=12, Mat[1][1]=15, Mat[2][1]=1 Mat[3][1]=21

Mat[0][2]=13, Mat[1][2]=16, Mat[2][2]=19 Mat[3][2]=22

9
While initializing we can group the elements row wise using inner

braces. for example:-

int mat[4][3]={{11,12,13},{14,15,16},{17,18,19},{20,21,22}};

And while initializing , it is necessary to mention the 2nd dimension where 1st
dimension is optional.
int mat[][3];

int mat[2][3];

int mat[][];

int mat[2][]; invalid

If we initialize an array as

int mat[4][3]={{11},{12,13},{14,15,16},{17}};

Then the compiler will assume its all rest value as 0,which are not defined.

Mat[0][0]=11, Mat[1][0]=12, Mat[2][0]=14, Mat[3][0]=17

Mat[0][1]=0, Mat[1][1]=13, Mat[2][1]=15 Mat[3][1]=0

Mat[0][2]=0, Mat[1][2]=0, Mat[2][2]=16, Mat[3][2]=0

In memory map whether it is 1-D or 2-D, elements are stored in


one contiguous manner.
We can also give the size of the 2-D array by using symbolic

constant Such as

#define ROW 2;

10
#define COLUMN 3;

int mat[ROW][COLUMN];

String:

Array of character is called a string. It is always terminated by the


NULL character. String is a one dimensional array of character.
We can initialize the string as

char name[]={„j‟,‟o‟,‟h‟,‟n‟,‟\o‟};

Here each character occupies 1 byte of memory and last character is always
NULL character. Where ‟\o‟ and 0 (zero) are not same, where ASCII value of „\o‟
is 0 and ASCII value of 0 is 48. Array elements of character array are also stored
in contiguous memory allocation.
From the above we can represent as;

J o h N„\o‟

The terminating NULL is important because it is only the way that


the function that work with string can know, where string end.
String can also be initialized as;

char name[]=”John”;

Here the NULL character is not necessary and the compiler will assume
it automatically.

String constant (string literal)

A string constant is a set of character that enclosed within the double quotes and
is also called a literal. Whenever a string constant is written anywhere in a
program it is stored somewhere in a memory as an array of characters terminated
by a NULL character („\o‟).

11
Example – “m”

“Tajmahal”

“My age is %d and height is %f\n”

The string constant itself becomes a pointer to the first character in array.

Example-char crr[20]=”Taj mahal”;

1000 1001 1002 1003 1004 1005 1006 1007 100 1009
T a j M A H a l \o
It is called base address.
String library function

There are several string library functions used to manipulate string and the
prototypes for these functions are in header file “string.h”. Several string
functions are
strlen()

This function return the length of the string. i.e. the number of characters in the
string excluding the terminating NULL character.
It accepts a single argument which is pointer to the first character of the string.

For example-

strlen(“suresh”);
It return the value 6.
In array version to calculate legnth:-

int str(char str[])


{
int i=0;
while(str[i]!=‟\o‟)
{
i++;
}

return i;

}
12
Example:-

#include<stdio.h>

#include<string.h>

void main()
{
char str[50];
print(”Enter a string:”);
gets(str);
printf(“Length of the string is %d\n”,strlen(str));

}
Output:

Enter a string: C in Depth

Length of the string is 8

strcmp()

This function is used to compare two strings. If the two string match, strcmp()
return a value 0 otherwise it return a non-zero value. It compare the strings
character by character and the comparison stops when the end of the string is
reached or the corresponding characters in the two string are not same.
strcmp(s1,s2)
return a value:
<0 when s1<s2
=0 when s1=s2
>0 when s1>s2

The exact value returned in case of dissimilar strings is not defined. We only know
that if s1<s2 then a negative value will be returned and if s1>s2 then a positive
value will be returned.

13
For example:
/*String comparison…………………….*/

#include<stdio.h>

#include<string.h>

void main()

char str1[10],str2[10];

printf(“Enter two strings:”);

gets(str1);

gets(str2);

if(strcmp(str1,str2)==0)

printf(“String are same\n”);

else

printf(“String are not same\n”);

strcpy()
This function is used to copying one string to another string. The function
strcpy(str1,str2) copies str2 to str1 including the NULL character. Here str2 is
the source string and str1 is the destination string.
The old content of the destination string str1 are lost. The function returns a pointer
to destination string str1.
14
Example:-

#include<stdio.h>

#include<string.h>

void main()

char str1[10],str2[10];

printf(“Enter a string:”);

scanf(“%s”,str2);

strcpy(str1,str2);

printf(“First string:%s\t\tSecond string:%s\n”,str1,str2);

strcpy(str,”Delhi”);

strcpy(str2,”Bangalore”);

printf(“First string :%s\t\tSecond string:%s”,str1,str2);

strcat()

This function is used to append a copy of a string at the end of the other string. If
the first string is “”Purva” and second string is “Belmont” then after using this
function the string becomes “PusvaBelmont”. The NULL character from str1 is
moved and str2 is added at the end of str1. The 2nd string str2 remains unaffected.
A pointer to the first string str1 is returned by the function.

Example:-

#include<stdio.h>
#include<string.h>
void main()
{
char str1[20],str[20];
printf(“Enter two strings:”);
gets(str1);

15
gets(str2);
strcat(str1,str2);
printf(“First string:%s\t second string:%s\n”,str1,str2);
strcat(str1,”-one”);
printf(“Now first string is %s\n”,str1);
}

Output

Enter two strings: data

Base
First string: database second string: database

` Now first string is: database-one

16
FUNCTION

A function is a self contained block of codes or sub programs with a set of


statements that perform some specific task or coherent task when it is called.
It is something like to hiring a person to do some specific task like, every
six months servicing a bike and hand over to it.

Modular Programming:
The process of subdividing a computer program into separate sub-programs
such as functions and subroutines is called Modular programming. Modular
programming sometimes also called as structured programming. It
enables multiple programmers to divide up the large program and debug
pieces of program independently and tested.
Then the linker will link all these modules to form the complete program. This
principle dividing software up into parts, or modules, where a module can be
changed, replaced, or removed, with minimal effect on the other software it works
with. Segmenting the program into modules clearly defined functions, it can
determine the source of program errors more easily. Breaking down program
functions into modules, where each of which accomplishes one function and
contains all the source code and variables needed to accomplish that function.
Modular program is the solution to the problem of very large program that are
difficult to debug, test and maintain. A program module may be rewritten while
its inputs and outputs remain the same. The person making a change may only
understand a small portion of the original program.

Any „C‟ program contain at least one function i.e main().

There are basically two types of function those are

1. Library function

2. User defined function

The user defined functions defined by the user according to its requirement

System defined function can‟t be modified, it can only read and can be used.
These function are supplied with every C compiler
Source of these library function are pre complied and only object code get used by
the user by linking to the code by linker
17
Here in system defined function description:

Function definition : predefined, precompiled, stored in the library


Function declaration : In header file with or function prototype.

Function call : By the programmer

User defined function

Syntax:-

Return type name of function (type 1 arg 1, type2 arg2, type3 arg3)

Return type function name argument list of the above syntax

So when user gets his own function three thing he has to know, these are.

Function declaration

Function definition

Function call

These three things are represented like

int function(int, int, int); /*function declaration*/

main() /* calling function*/

function(arg1,arg2,arg3);

int function(type 1 arg 1,type2 arg2,type3, arg3) /*function definition/*

Local variable declaration;

Statement;

Return value;

18
Function declaration:-
Function declaration is also known as function prototype. It inform the compiler
about three thing, those are name of the function, number and type of argument
received by the function and the type of value returned by the function.
While declaring the name of the argument is optional and the function
prototype always terminated by the semicolon.

Function definition:-

Function definition consists of the whole description and code of the function.

It tells about what function is doing what are its inputs and what are its out put

It consists of two parts function header and function body


Syntax:-

return type function(type 1 arg1, type2 arg2, type3 arg3) /*function header*/

Local variable declaration;

Statement 1;

Statement 2;

Return value

The return type denotes the type of the value that function will return and it is
optional and if it is omitted, it is assumed to be int by default. The body of the
function is the compound statements or block which consists of local variable
declaration statement and optional return statement.

The local variable declared inside a function is local to that function only. It can‟t
be used anywhere in the program and its existence is only within this function.
The arguments of the function definition are known as formal arguments.

19
Function Call

When the function get called by the calling function then that is called, function
call. The compiler execute these functions when the semicolon is followed by
the function name.
Example:-

function(arg1,arg2,arg3);

The argument that are used inside the function call are called actual argument

Ex:-

int S=sum(a, b); //actual arguments

Actual argument

The arguments which are mentioned or used inside the function call is knows
as actual argument and these are the original values and copy of these are
actually sent to the called function
It can be written as constant, expression or any function call like

Function (x);

Function (20, 30);

Function (a*b, c*d);

Function(2,3,sum(a, b));

Formal Arguments

The arguments which are mentioned in function definition are called


formal arguments or dummy arguments.

These arguments are used to just hold the copied of the values that are sent by
the calling function through the function call.
These arguments are like other local variables which are created when the
function call starts and destroyed when the function ends.

20
The basic difference between the formal argument and the actual argument are

1). The formal argument are declared inside the parenthesis where as the
local variable declared at the beginning of the function block.
2). The formal argument are automatically initialized when the copy of
actual arguments are passed while other local variable are assigned values
through the statements.
Order number and type of actual arguments in the function call should be
match with the order number and type of the formal arguments.
Return type
It is used to return value to the calling function. It can be used in two way as

return

Or return(expression);

Ex:- return (a);

return (a*b);

return (a*b+c);

Here the 1st return statement used to terminate the function without returning
any value
Ex:- /*summation of two values*/

int sum (int a1, int a2);

main()
{

int a,b;

printf(“enter two no”);

scanf(“%d%d”,&a,&b);

int S=sum(a,b);

printf(“summation is = %d”,s);

}
21
int sum(intx1,int y1)

int z=x1+y1;

Return z;

}
Advantage of function

By using function large and difficult program can be divided in to sub programs
and solved. When we want to perform some task repeatedly or some code is to
be used more than once at different place in the program, then function avoids
this repeatition or rewritten over and over.
Due to reducing size, modular function it is easy to modify and test.

Notes:-

C program is a collection of one or more function.

A function is get called when function is followed by the semicolon.

A function is defined when a function name followed by a pair of curly braces

Any function can be called by another function even main() can be called by
other function.
main()
{

function1()

function1()

Statement;

function2;

22
}

function 2()

So every function in a program must be called directly or indirectly by the


main() function. A function can be called any number of times.
A function can call itself again and again and this process is called recursion.

A function can be called from other function but a function can‟t be defined in
another function

Category of Function based on argument and return type

i) Function with no argument & no return value

Function that have no argument and no return value is written as:-void

function(void);

main()
{
void function()
{
Statement;
}
Example:-
void me();
main()
{
me();
printf(“in main”);
}
void me()
{
printf(“come on”);
}

Output: come on

23
inn main
ii) Function with no argument but returnvalue

Syntax:-

int fun(void);

main()

{
int r;

r=fun();

}
int fun()

reurn(exp);

}
Example:-

int sum();

main()

int b=sum();

printf(“entered %d\n, b”);

}
int sum()

int a,b,s;
s=a+b;
return s;
}

24
Here called function is independent and are initialized. The values aren‟t passed
by the calling function .Here the calling function and called function are
communicated partly with each other.
iii ) function with argument but no return value

Here the function have argument so the calling function send data to the called
function but called function dose n‟t return value.
Syntax:-

void fun (int,int);

main()

int (a,b);

void fun(int x, int y);

Statement;

Here the result obtained by the called function.

iv) function with argument and return value

Here the calling function has the argument to pass to the called function and
the called function returned value to the calling function.
Syntax:-
fun(int,int);
main()
{

int r=fun(a,b);
}
int fun(intx,inty)
{

return(exp);
25
}

Example:

main()

int fun(int);

int a,num;

printf(“enter value:\n”);

scanf(“%d”,&a)
int num=fun(a);

}
int fun(int x)
{
++x;
return x;

Call by value and call by reference:

There are two way through which we can pass the arguments to the function
such as call by value and call by reference.
1. Call by value

In the call by value copy of the actual argument is passed to the formal argument
and the operation is done on formal argument.
When the function is called by „call by value‟ method, it doesn‟t affect content of
the actual argument.
Changes made to formal argument are local to block of called function so when the
control back to calling function the changes made is vanish.

26
Example:-

main()

int x,y;

change(int,int);
printf(“enter two values:\n”);

scanf(“%d%d”,&x,&y);

change(x ,y);

printf(“value of x=%d and y=%d\n”,x ,y);

change(int a,int b);

int k;

k=a;

a=b;

b=k;

Output: enter two values: 12

23

Value of x=12 and y=23


2. Call by reference

Instead of passing the value of variable, address or reference is passed and the
function operate on address of the variable rather than value.
27
Here formal argument is alter to the actual argument, it means formal
arguments calls the actual arguments.
Example:-

void main()
{
int a,b;

change(int *,int*);

printf(“enter two values:\n”);

scanf(“%d%d”,&a,&b);

change(&a,&b);

printf(“after changing two value of a=%d and b=%d\n:”a,b);

}
change(int *a, int *b)

int k;

k=*a;

*a=*b;

*b= k;

printf(“value in this function a=%d and b=%d\n”,*a,*b);

Output: enter two values: 12

32

Value in this function a=32 and b=12

After changing two value of a=32 and b=12

28
So here instead of passing value of the variable, directly passing address of the
variables. Formal argument directly access the value and swapping is possible
even after calling a function.

Local, Global and Static variable

Local variable:-

variables that are defined with in a body of function or block. The local
variables can be used only in that function or block in which they are
declared. Same variables may be used in different functions such as
function()

{
int a,b;
function 1();
}
function2 ()
{
int a=0;
b=20;
}
Global variable:-
The variables that are defined outside of the function is called global variable. All
functions in the program can access and modify global variables. Global variables
are automatically initialized at the time of initialization.
Example:
#include<stdio.h>
void function(void);
void function1(void);
void function2(void);
int a, b=20;
void main()
{
printf(“inside main a=%d,b=%d \n”,a,b);
function();
function1();
function2();
29
}
function()
{
Prinf(“inside function a=%d,b=%d\n”,a,b);
}

function 1()

{
prinf(“inside function a=%d,b=%d\n”,a,b);
}
function 2()
{
prinf(“inside function a=%d,b=%d\n”,a,);
}
Static variables: static variables are declared by writing the key word static.

-syntax:-

static data type variable name;

static int a;

-the static variables initialized only once and it retain between the function call. If
its variable is not initialized, then it is automatically initialized to zero.
Example:

void fun1(void);

void fun2(void);

void main()

{
fun1();
fun2();
}
void fun1()
{
int a=10, static int b=2;

printf(“a=%d, b=%d”,a,b);
a++;
b++;
30
}
Output:a= 10 b= 2
a=10 b= 3

Recursion

When function calls itself (inside function body) again and again then it is
called as recursive function. In recursion calling function and called function are
same. It is powerful technique of writing complicated algorithm in easiest way.
According to recursion problem is defined in term of itself. Here statement with in
body of the function calls the same function and same times it is called as circular
definition. In other words recursion is the process of defining something in form
of itself.
Syntax:
main ()

{
rec(); /*function call*/
rec();
rec();

Ex:- /*calculate factorial of a no.using recursion*/

int fact(int);

void main()
{
int num;
printf(“enter a number”);
scanf(“%d”,&num);
f=fact(num);
printf(“factorial is =%d\n”f);
}
fact (int num)
{
If (num==0||num==1)
return 1;
else
return(num*fact(num-1));
}

31
Storage Classes:

Storage class in c language is a specifier which tells the compiler where and how to
store variables, its initial value and scope of the variables in a program. Or
attributes of variable is known as storage class or in compiler point of view a
variable identify some physical location within a computer where its string of bits
value can be stored is known as storage class.
The kind of location in the computer, where value can be stored is either in the
memory or in the register. There are various storage class which determined, in
which of the two location value would be stored.
Syntax of declaring storage classes is:-

storageclass datatype variable name;

There are four types of storage classes and all are keywords:-

1 ) Automatic (auto)
2 ) Register (register)
3) Static (static)
4 ) External (extern)
Examples:-
auto float x; or float x;
extern int x;
register char c;
static int y;

Compiler assume different storage class based on:-


1. Storage class:- tells us about storage place(where variable would be stored).
2. Intial value :-what would be the initial value of the variable.If initial value
not assigned, then what value taken by uninitialized variable.
3. Scope of the variable:-what would be the value of the variable of the program.
4. Life time :- It is the time between the creation and distribution of a variable or
how long would variable exists.

32
1. Automatic storage class

The keyword used to declare automatic storage class is auto.


Its features:-

Storage-memory location

Default initial value:-unpredictable value or garbage value.


Scope:-local to the block or function in which variable is defined.

Life time:-Till the control remains within function or block in which it is defined.
It terminates when function is released.

The variable without any storage class specifier is called automatic variable.

Example:-

main( )

auto int i;

printf(“i=”,i);

2. Register storage class

The keyword used to declare this storage class is register.

The features are:-

Storage:-CPU register.

Default initial value :-garbage value

Scope :-local to the function or block in which it is defined.

Life time :-till controls remains within function or blocks in which it is defined.

33
Register variable don‟t have memory address so we can‟t apply address operator
on it. CPU register generally of 16 bits or 2 bytes. So we can apply storage
classes only for integers, characters, pointer type.
Variable stored in register storage class always access faster than,which is
always stored in the memory. But to store all variable in the CPU register is not
possible because of limitation of the register pair.
And when variable is used at many places like loop counter, then it is better
to declare it as register class.
Example:-

main( )

register int i;

for(i=1;i<=12;i++)

printf(“%d”,i);

3 . Static storage class


The keyword used to declare static storage class is static.

Its feature are:-

Storage:-memory location.

Default initial value:- zero.

Scope :- local to the block or function in which it is defined.

Life time:- value of the variable persist or remain between different function call.

Example:-

main( )
{
reduce( );
reduce( );
reduce ( );
34
}

reduce( )
{
static int x=10;
printf(“%d”,x);
x++;
}

Output:-10,11,12

4.External storage classes

The keyword used for this class is extern.

Features are:-

Storage:- memory area.

Default initial value:-zero.

Scope :- global

Life time:-as long as program execution remains it retains.


Declaration does not create variables, only it refer that already been created
at somewhere else. So, memory is not allocated at a time of declaration and
the external variables are declared at outside of all the function.
Example:-

int i,j;

void main( )

printf( “i=%d”,i );

receive( );

receive ( );

reduce( );
35
reduce( );

receive( )

i=i+2;

printf(“on increase i=%d”,i);

reduce( )

i=i-1;

printf(“on reduce i=%d”,i);

Output:-i=0,2,4,3,2.

When there is large program i.e divided into several files, then external
variable should be preferred. External variable extend the scope of variable.

36

You might also like