0% found this document useful (0 votes)
9 views23 pages

Storage Class in C

Uploaded by

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

Storage Class in C

Uploaded by

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

JUSTICE BASHEER AHMED SAYEED COLLEGE FOR

WOMEN
(Autonomous) Afternoon Session Chennai 18.
S.I.E.T.

Prepared by
M. MINU MEERA, Assistant Professor
A. JUNAITHA BARVEEN, Assistant
Professor

06/22/24 Presenter Name: M.MINU MEERA DEPARTMENT OF COMPUTER


,A.JUNAITHA BARVEEN, Department SCIENCE 1
of Computer Science
Every ‘C’ variable has a characteristic called its Storage Class.
All variables have datatype and storage classes

 Keyword
 Where it is Declared
 Storage Area
 Default Initial value
 Lifetime of a variable

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 2


of Computer Science
1. Local or Auto or Internal variable
2. External or Global variable
3. Static variable
4. Register Variable

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 3


of Computer Science
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 4
of Computer Science
#include<stdio.h>
void function1();
void function2();
void main()
Auto variable are always declared within a {
int m=1000;
function and they are local to the function in function2();
which they are declared. Hence they are also printf("%d\n",m);
named as local variables }

void function1()
Keyword : auto {
Declaration : Inside the function int m=10;
Storage Area : Stack printf("%d\n",m);
}
Initial Value : Garbage value (At the
time of compilation compiler assigns any value) void function2()
Lifetime : Upto that function only {
int m=100;
function1();
Example : printf("%d\n",m);
auto int x; (or) int x; }
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 5
of Computer Science
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 6
of Computer Science
#include<stdio.h>
int k;
void function1();
void function2();
A variable which can be access with in a void function3();
void main()
function and outside the main function.
{
These variables are also named as Global k=20;
variables or External variables function1();
Keyword : extern function2();
Declaration : Outside of the main() function function3();
}
Storage Area : CPU–memory void function1() {
Initial Value : zero k=k+10;
printf("%d\n",k); }
Lifetime : Upto the entire program void function2()
Example : {
k=k+1000;
int x; printf("%d\n",k);
main() main() }
void function3()
{ { {
(or) extern int x; k=k+10;
printf("%d\n",k);
} } }
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department of Computer Science 7
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 8
of Computer Science
/* To print the value of x */
#include<stdio.h>
void stat();
This variable static is constant and the value void main()
is continued in all the steps. {
Keyword : static int i;
Declaration : Inside the function for(i=1;i<=5;i++)
stat(); //calling
Storage Area : CPU – memory
}
Initial Value : Zero void stat() //definition
Lifetime : The value of the variable persists {
static int x=0;
between different function calls.
printf("x=%d\n",x);
Example : x=x+1;
static int x; }

0
1
2
3
4
5
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department
of Computer Science 9
/* Example 2 */
#include<stdio.h>
#include<conio.h>
void incre(); /* Function prototype declaration */
void main()
{
clrscr();
incre();
incre(); The character Stored in x is A
incre();
getch(); The character Stored in x is B
}
The character Stored in x is C
void incre()
{
static char x=65;
printf("\n The character stored in x is %c",x++);
}

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 10


of Computer Science
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 11
of Computer Science
#include<stdio.h>
#include<conio.h>
These variables are stored in CPU registers and hence
void main()
they can be accessed faster than the one which is stored
{
in memory. register int x;
Keyword : register clrscr();
Declaration : Inside the function printf("\n The value is %d",x);
Storage Area : CPU - Register getch();
Initial Value : Garbage value(At the time of }
compilation compiler assigns any value)
Lifetime : Upto that function only
Example : register int x;
Note :
register double x; register float y;
Registers are usually a 16bit therefore it -899
cannot hold a float or double data type value which (Garbage Value)
require 52 & 64 bytes respectively for storing a value.
But the compiler would treat as automatic variables

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 12


CSC COMPUTER EDUCATION,
M.K.B.NAGAR of Computer Science
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 13
of Computer Science
Functions communicate with each
other by passing arguments.
It can be passed in Two Ways
1.Call By Value
2. Call by Reference

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 14


of Computer Science
Passing
Parameters

Call by value
Call by reference

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 15


of Computer Science
 The values are passed through
temporary variables. Any manipulation to
be done only on these temporary variables.

 The called function does not access the


actual memory location of the original
variable and therefore cannot change its
value.

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 16


of Computer Science
/* CALL BY VALUE EXAMPLE*/
#include<stdio.h>
void add(int,int);
void main()
{
int x,y; Enter two number = 60
20
printf("Enter two number"); The C Value is 80
scanf("\t\t%d %d",&x,&y);
add(x,y);
}
void add(int a,int b)
{
int c=a+b;
printf("\t\tThe C Value is %d",c);
}
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department
CSC COMPUTER EDUCATION,
M.K.B.NAGAR of Computer Science
17
 The function is allowed access the actual
memory location(Address) of the argument
(original variable) and therefore can change
the value of the arguments of the calling
routine have to be changed.

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 18


of Computer Science
/*CALL BY REFERENCE as well as to swap 2 numbers
using pointers */
#include<stdio.h>
void swap(int *,int *);
void main()
{
int a,b;
printf("\nEnter the numbers to swap");
scanf("%d %d",&a,&b);
printf("The values before swapping :");
printf("\n%d %d",a,b);
swap(&a,&b);
}
Continue….
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 19
of Computer Science
void swap(int *e,int *f)
{
int *temp;
*temp=*e;
*e=*f;
*f=*temp;
printf("\n The swapped values are %d %d",*e,*f);
}

Enter the numbers to swap = 5 NOTE :


6
The values before swapping : 5 6 &  Address of
The swapped values are : 6 5
*  Content of

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 20


of Computer Science
Functions are easier to write and understand

 The arguments are seperated by commas


 The body of the function may consist of one or many
statements
 It cannot be defined within another function
 Function prototype is a function declaration that specifies
the data types of the arguments
 Calling one function from within another is said to be
nesting of Function calls
 main() returns an integer which is generally the operating
system
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 21
of Computer Science
Session Summary

 A function is a self contained program segment (block of statements) that performs

some specific well defined task.

Three steps in using a function are defining a function, prviding a prototype and calling

the function.

 Return statement is used to return the information from the function to the calling

portion of the program

 Scope of a variable is defined as the region over which the variable is visible or valid.

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 22


of Computer Science
EXERCISES

1. Write a program to sort the numbers in ascending order using functions?

2. Write a program to calculate xn using functions?

3. Write a program to check whether the year is leap year or not using functions?

4. Write a program to find the square of first N Numbers and to calculate its sum?

5. Write a program to swap two numbers using functions?

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 23


of Computer Science

You might also like