Semester : S1 and S2
Subject : Computer Programming
Year : 2017
Term : DECEMBER
Branch : BIOTECHNOLOGY
Scheme : 2015 Full Time
Course Code : CS 100
Page:49
1. External Storage Class
The extern specifier gives the declared variable external storage class. The
principal use of extern is to specify that a variable is declared with
external linkage elsewhere in the program. To understand why this is
important, it is necessary to understand the difference between a declaration
and a definition. A declaration declares the name and type of a variable or
function. A definition causes storage to be allocated for the variable or the
body of the function to be defined. The same variable or function may have
many declarations, but there can be only one definition for that variable or
function.
When extern specifier is used with a variable declaration then no
storage is allocated to that variable and it is assumed that the variable has
already been defined elsewhere in the program. When we use extern
specifier the variable cannot be initialized because with extern specifier
variable is declared, not defined.
In the following sample © program if you remove extern int x; you
will get an error "Undeclared identifier ^^ because variable > is defined
later than it has been used in printf. In this example, the extern
specifier tells the compiler that variable > has already been defined and it 15
declared here for compiler's information.
#include
main ()
( printf("x: %d\n ",
29) 7
Also, if you change the statement extern int x; toextern int x
= 50; you will again get an error "Redefinition of ^^ because with
extern specifier the variable cannot be initialized, if itis defined
elsewhere. If not then extern declaration becomes a definition.
Note that extern can also be applied to a function declaration, but doing so
is redundant because all function declarations are implicitly extern.
0) Write a © program to read name and marks of n number of students from user and store them in
a file. If the file previously exits, add the information of n students.
#include
int main() {
char name [50];
int marks,i,n;
printf ("Enter
number of
students: ");
scanf ("%d",&n);
FILE *fptr;