You are on page 1of 17

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 : External variables.
Teaching Aids : PPT, Animations

CM304.67 1
Objective

On completion of this period, you would be able to


know :
• Understand external variables.

• Understand the importance of Extern declaration.

CM304.67 2
Recap

In the previous lesson, you have learnt about..

• Automatic Variable.

• Static Variable.

• Scope of automatic & static variables.

CM304.67 3
External variables

• Function as global variables.

• Scope extends from the point of definition


through the remainder of the program.

• Retain their assigned values till the end of the


program.

CM304.67 4
External variables
Contd..

• Defined outside any function and available to


many functions.

• Can be assigned values within one function


and these values can be used within another
function.

CM304.67 5
External Declaration

• Variables can be referenced by functions that


are defined in different source files, by
defining the variables in the first file and
explicitly declaring them in the remaining
source files using the storage specifier
‘extern’.

CM304.67 6
External Declaration Contd..
Example :
int value;
main()
{
--------
printf(“enter a value\n”);
scanf(“%d”,&value);
calc();
}
/* PROGRAM A */
CM304.67 7
External Declaration
Contd..
calc()

extern int val;

--------

--------

/* PROGRAM B */
CM304.67 8
External Declaration
Contd..

• The variable value is declared as global type


in program A.

• It has been declared again within function


calc() in program B with the storage specifier
‘extern’.

CM304.67 9
External Declaration
Contd..

• The ‘extern’ declaration in program B


indicates to the compiler that the variable ‘val’
used in this program file has been declared in
another program file.

CM304.67 10
‘Extern’ declaration

• The ‘extern’ declaration does not cause


memory to be reserved for the variable.

• Memory is reserved when the variable is


declared as global and the same variable
cannot have two different memory locations.

CM304.67 11
Summary

In this class, we have learnt about..

• External variables.

• Importance of storage specifier ‘extern’.

CM304.67 12
Quiz

1. Scope of the external variables is..

a) Within the function

c) Throughout the program

e) None

CM304.67 13
Quiz

1. Scope of the external variables is..

• Within the function

• Throughout the program

• None

CM304.67 14
Quiz

2. The extern declaration does not cause


memory to be reserved for the variable..

b) Yes

d) No

f) Not known

CM304.67 15
Quiz

2. The extern declaration does not cause


memory to be reserved for the variable..

 Yes

 No

 Not known

CM304.67 16
Frequently Asked Questions

1.Explain about external storage class specifier


with an example.

2.Write short note on storage classes?


(Apr07)

CM304.67 17

You might also like