You are on page 1of 19

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRA PRADESH
Name : Murali Krishna Chintala
Designation : Lecturer in CME
Branch : Computer Engineering
Institution : 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 : Local and External variables.
Teaching Aids : PPT, Animations

CM304.65 1
Objective

On completion of the class, you would be able


to know
• Understand Local variables.

• Understand Global variables.

• Differentiate Local and Global variables.

CM304.65 2
Recap

In the previous class, you have learnt about..

• Purpose of function prototype.

CM304.65 3
Local variables

• Variables which are defined within the body of


the function.

• Variables local to that function block.

CM304.65 4
Local variables
Contd..
Example:-
var(int i,int j)
{
int a,b;
---------
}

• a, b are called local variables as they are


defined within the body of the function var().

CM304.65 5
Local variables
Contd..

• Can only be used in the function in


which they are defined.

• Same variable names can be used in


different functions as they are local to
that particular function only.

CM304.65 6
Global variables

• Variables which are defined outside main()


function.
• Global variables are also known as External
variables.

CM304.65 7
Global variables
Contd..

• Global variables has the same name and


same data type throughout the program.

• It is useful to declare a variable as Global


when it has constant value throughout the
the program.

CM304.65 8
Global variables
Contd..

Example:-
#include<stdio.h>
int a,b=6;
void fun();
main()
{
a=8;
fun();
}

CM304.65 9
Global variables
Contd..

void fun()
{
int area;
area = a*b;
printf(“%d”, area);
}

CM304.65 10
Global variables
Contd..

• ‘a’ and ‘b’ are global variables as they are


defined outside the main function.

• ‘area’ is local variable as it is defined within


the function fun().

CM304.65 11
Differences between local and global
variables
Local variables Global variables

1) Also known as Known as External


Automatic variables. variables.

2) Scope is within the Scope is entire


function. program.

3) Defined within the Defined outside


function. main() function.
CM304.65 12
Local variables Global variables
Contd..
4)Same variables can be Variables have constant
used in different values throughout the
programs. program.

5) Created upon Exists as long as


entry into its the program is
block and destro- executing.
yed upon exit.

CM304.65 13
Summary
In this class, you have learnt about..

• Variables which are defined within the body of the


function are called Local variables.

• Variables which are defined outside main function


are called Global variables.

• Differences between Local and Global variables.

CM304.65 14
Quiz
1. Variables declared within the function body
are

c) Local variables

e) Global variables

g) None

CM304.65 15
Quiz
1. Variables declared within the function body
are

• Local variables

• Global variables

• None

CM304.65 16
Quiz
2. Variables declared outside the main function are..

a) Local variables

b) Global variables

c) None

CM304.65 17
Quiz
2. Variables declared outside the main function are..

a) Local variables

b) Global variables

c) None

CM304.65 18
Frequently Asked Questions

1.Explain about Local and Global variables.

2.Write the differences between the Local and Global


variables?

CM304.65 19

You might also like