You are on page 1of 35

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRA PRADESH
Name : Murali Krishna Chintala
Designation : Lecturer in CME
Branch : Computer Engineering
Institute : SUVR & SR GPW, Ethamukkala
Year/Semester : III Semester
Subject Name : UNIX & C
Subject Code : CM – 304
Major Topic : Understand Modular Programming
Duration : 50 Min
Sub Topic : Automatic & Static variables.
Teaching Aids : PPT, Animations

CM304.66 1
Objective

On completion of the class, you would be able to


know:

• Automatic variables.

• Static variables.

• Scope of automatic and static variables.

CM304.66 2
Recap

In the previous class, you have learnt..

• Understand Local Variables.

• Understand Global Variables.

• Differences between local & global variables.

CM304.66 3
Scope of a variable

• Area or block of program from where the


variable can be accessed is known as the scope
of the variable.

• Scope of the variable depends on it’s storage


class.

CM304.66 4
Storage class of the variable

• The storage area of the variable.


• Initial value of the variable.
• Scope of the variable.
• Life of the variable.

CM304.66 5
Storage classes

Any variable declared in ‘C’ has one of the


four storage classes :

• Automatic
• External
• Static
• Register

CM304.66 6
Automatic variables

• Variables declared with or without a storage


class specification ‘auto’.

• ‘auto’ is the default storage class for local


variables.

CM304.66 7
Scope of automatic variables

• Limited to the block of the function in which they


are declared.

• Once the execution of the function is completed,


these variables are disposed off.

CM304.66 8
Automatic variables

Syntax :
auto data-type variable list;
OR
data-type variable list;

Example :
auto int a,b,c; OR int a,b,c;

CM304.66 9
Automatic variables
Example:
#include< stdio.h>
main()
{
void fun();
auto int x=10,y=20;
printf (“in main program the value of x is
%d and value of y is %d\n”x,y);
fun();
}
CM304.66 10
Automatic variables Contd..

Example:-
void fun()
{
int x =100,y=200;
printf(“in function fun the value of x is %d
and value of y is %d\n”,x,y);
}

CM304.66 11
Automatic variables
Contd..

Output:

• In main the value of x is 10 and value of y is


20.

• In function fun the value of x is 100 and value


of y is 200.

CM304.66 12
Automatic variables
Contd..

• Scope of x and y is limited to main.

• In other words they are limited to main.

• Variables declared inside function fun are also


local to the function fun().

CM304.66 13
Features of automatic variables

• Storage space : main memory

• Default initial value : garbage

• Scope : local to the block in which it


is defined.
• Life : remains till the execution of
the block in which it is
declared.

CM304.66 14
Static variables

• Retains the latest value, when the block or


function in which they are declared is
reinvoked.

• When declared as static ,the variable is


initialized to null value instead of garbage.

CM304.66 15
Static variables Contd..

• Will be initialized only once.

• Contents of static variables remain constant


throughout program execution.

CM304.66 16
Static variables
Contd..

Syntax :

static data-type variable list;

Example :
static int x;

CM304.66 17
Scope of static variables

• Limited to the block or function in which they


are declared.

• Static is the default storage class for global


variables.

CM304.66 18
Types of static variables

• Internal or local static variables.

• External or global static variables.

CM304.66 19
Scope of internal/external static variables

• Scope of internal or local static variables is


local to the function in which they appear.

• Scope of external or global static variables is


throughout the program.

CM304.66 20
Scope of internal/external static variables
Contd..

Example :

#include<stdio.h>
void add();
main()
{
add();
}
void add()
{static int x;
x=x+1;
printf(“the value of x is %d\n”,x);
}

CM304.66 21
Scope of internal/external static variables
Contd..
Output
The value of x is 1

The value of x is 2

• Since x is defined as static in function add it’s


value is initialized to null at the time of
declaration,hence the output is 1 for the first
time

CM304.66 22
Scope of internal/external static variables
Contd..

Output

• Since x is a static variable it retains it’s latest


value.

• When add is called second time, the output is 2.

CM304.66 23
Example for global static variables
#include<stdio.h>
static int no;
main()
{
no=5;
printf(“in main() no=%d\n”,no);
fun1();
fun2();
printf(“after function call no=%d\n”,no);
}

CM304.66 24
void fun1() Contd..

{
int x=5;
no=no+x;
printf(“in fun1() no=%d\n”,no);
}
void fun2()
{
int x=5;
no=no+x;
printf(“in fun2() no=%d\n”,no);
}

CM304.66 25
Scope of internal/external static variables
Contd..
Output
• In main() no = 5
• In fun1() no = 10
• In fun2() no = 15
• After function call no = 15
• Since ‘no’ is declared as a global static
variable it retains it’s value throughout the
program.

CM304.66 26
Features of static variables
• Storage space : main memory

• Default initial value : zero

• Scope : local to block in which it is


defined.

• Life : variable value persists


between different function
calls.

CM304.66 27
Summary

In this class, we have discussed about :

• Automatic variables.

• Static variables.

• Scope of automatic and static variables.

CM304.66 28
Quiz

1. Default value of automatic variable is..

a) Zero

c) Garbage

e) None

CM304.66 29
Quiz

1. Default value of automatic variable is..

a) Zero

b) Garbage

c) None

CM304.66 30
Quiz
2. Default value of static variable is..

a) Zero

c) Garbage

e) None

CM304.66 31
Quiz

2. Default value of static variable is..

 Zero

 Garbage

 None

CM304.66 32
Quiz

3. Scope of automatic variables is..

a) Within the function

c) Throughout the program

e) Not defined

CM304.66 33
Quiz

3. Scope of automatic variables is..

 Within the function

 Throughout the program

 Not defined

CM304.66 34
Frequently Asked Questions

1. Explain automatic and static storage classes


with examples? (sep’02,apr’03,apr’04)

2. Compare different types of storage


classes?(apr’05)

CM304.66 35

You might also like