Documente Academic
Documente Profesional
Documente Cultură
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
• Automatic variables.
• Static variables.
CM304.66 2
Recap
CM304.66 3
Scope of a variable
CM304.66 4
Storage class of the variable
CM304.66 5
Storage classes
• Automatic
• External
• Static
• Register
CM304.66 6
Automatic variables
CM304.66 7
Scope of automatic variables
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:
CM304.66 12
Automatic variables
Contd..
CM304.66 13
Features of automatic variables
CM304.66 14
Static variables
CM304.66 15
Static variables Contd..
CM304.66 16
Static variables
Contd..
Syntax :
Example :
static int x;
CM304.66 17
Scope of static variables
CM304.66 18
Types of static variables
CM304.66 19
Scope of internal/external static variables
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
CM304.66 22
Scope of internal/external static variables
Contd..
Output
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
CM304.66 27
Summary
• Automatic variables.
• Static variables.
CM304.66 28
Quiz
a) Zero
c) Garbage
e) None
CM304.66 29
Quiz
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
Zero
Garbage
None
CM304.66 32
Quiz
e) Not defined
CM304.66 33
Quiz
Not defined
CM304.66 34
Frequently Asked Questions
CM304.66 35