SlideShare una empresa de Scribd logo
Also See more posts : www.comsciguide.blogspot.com
Extern :
All variables we have seen so far have limited scope (the block
in which they are declared) within the program. However, in some
applications it may be useful to have data which is accessible from
within any other file or which remains in existence for the entire
execution of the program.
Suppose if a variable is declared in file1 and we want to access
the variable in another file2 and file3, then how to access these
variables in other files??? Simple, the answer to this is “Extern”.
Extern keyword is used for the global variables that are defined
in one file and can be used those variables in other files by using
extern keyword.
extern int a;
Let us see an example:
file1.c file2.c
#include<stdio.h> #include<stdio.h>
int main() int a=1;
{ int main()
extern int a; {return 0;}
printf("%d",a);
return 0;
}
In this example, the variable ‘a‘ is created and initialized in
file2.c and is used in file1.c as external variable. For information on
how to compile a program whose source code is split among multiple
files, you can simply type the command gcc -o a file1.c file2.c, and
run the program with ./a.
 When you use extern keyword before the global variable
declaration, the compiler understands you want to access a
Also See more posts : www.comsciguide.blogspot.com
variable being defined in another program or file, and hence
not to allocate any memory for this one. Instead, it simply
points to the global variable defined in the other file.
a)
#include <stdio.h>
extern int i; //extern variable
int main()
{
printf("%d",i);
return 0;
}
Output: Compilation error, undefined symbol i.
b)
#include<stdio.h>
extern int i;
int main()
{
i = 1;
return 0;
}
This program throws error in compilation. Because ‘i’ is
declared but not defined anywhere. Essentially, the ‘I’ isn’t
allocated any memory. In (b), the program is trying to change
the value to 1 of a variable that doesn’t exist at all.
 If the global variables are not initialized then, they are
initialized to their default values.
Int – 0 , Float – 0.0, Char - 0 , String – null
#include <stdio.h>
int i; char c; float f; char *str;
int main()
{
printf("%d %d %f %s",c,i,f,str);
Also See more posts : www.comsciguide.blogspot.com
return 0;
}
Output: 0 0 0.000000 (null)
 If we declare a variable as extern, it means that the definition
for that variable is defined somewhere else in the program or
in other file. It doesn’t allocate the memory for these variables.
It’s just the declaration. So we can declare it as many times as
we can.
#include <stdio.h>
extern int i; //Declaring the variable i.
int i=1; //Initializing the variable.
extern int i; //Again declaring the variable i.
int main()
{
extern int i; //Again declaring the variable i.
printf("%d",i);
return 0;
}
Output: 1
 A particular extern variable can be declared many times but we
can initialize at only one time. (even outside or inside the
function of same program)
#include <stdio.h>
extern int i; //Declaring the variable
int i=25; //Initializing the variable
int main()
{
printf("%d",i);
return 0;
}
int i=20; //Initializing the variable
Output: Compilation error: Multiple initialization
variable i.
Also See more posts : www.comsciguide.blogspot.com
 To define a variable i.e. allocate the memory for extern
variables it is necessary to initialize the variables.
#include <stdio.h>
extern int i=1; //extern variable
int main()
{
printf("%d",i);
return 0;
}
Output: 1
 We cannot initialize extern variable locally i.e. within any block
either at the time of declaration or separately. We can only
initialize extern variable globally.
a)
#include <stdio.h>
int main()
{
extern int i=1; // Try to initialize extern variable
// locally.
printf("%d",i);
return 0;
}
Output: Compilation error: Cannot initialize extern
variable.
b)
#include <stdio.h>
int main()
{
extern int i; // Declaration of extern variable i.
int i=1; // Try to locally initialization of
// extern variable i.
printf("%d",i);
return 0;
}
Output: Compilation error: Multiple declaration of
variable i.
Also See more posts : www.comsciguide.blogspot.com
For more topics Visit :
http://comsciguide.blogspot.com/

Más contenido relacionado

La actualidad más candente

Looping statement
Looping statementLooping statement
Looping statement
ilakkiya
 
Fundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medinaFundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medina
rurumedina
 

La actualidad más candente (15)

Java 8 - CJ
Java 8 - CJJava 8 - CJ
Java 8 - CJ
 
Loops in c
Loops in cLoops in c
Loops in c
 
Spf Chapter5 Conditional Logics
Spf Chapter5 Conditional LogicsSpf Chapter5 Conditional Logics
Spf Chapter5 Conditional Logics
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
PHP - Introduction to PHP Error Handling
PHP -  Introduction to PHP Error HandlingPHP -  Introduction to PHP Error Handling
PHP - Introduction to PHP Error Handling
 
Looping statement
Looping statementLooping statement
Looping statement
 
Looping statements
Looping statementsLooping statements
Looping statements
 
Fundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medinaFundamentals of prog. by rubferd medina
Fundamentals of prog. by rubferd medina
 
Conditionalstatement
ConditionalstatementConditionalstatement
Conditionalstatement
 
Exception handling
Exception handlingException handling
Exception handling
 
Artificial Intelligence and Expert System
Artificial Intelligence and Expert System Artificial Intelligence and Expert System
Artificial Intelligence and Expert System
 
Introduction to Prolog
Introduction to Prolog Introduction to Prolog
Introduction to Prolog
 
Modula-2 tutorial - 001 - my fisrt program
Modula-2 tutorial - 001 - my fisrt programModula-2 tutorial - 001 - my fisrt program
Modula-2 tutorial - 001 - my fisrt program
 
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
 
Php variables
Php variablesPhp variables
Php variables
 

Destacado

Variables 9 cm604.7
Variables 9 cm604.7Variables 9 cm604.7
Variables 9 cm604.7
myrajendra
 
constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in C
Sahithi Naraparaju
 
Chapter1 c programming data types, variables and constants
Chapter1 c programming   data types, variables and constantsChapter1 c programming   data types, variables and constants
Chapter1 c programming data types, variables and constants
vinay arora
 
Array in c language
Array in c languageArray in c language
Array in c language
home
 
Operating system and its function
Operating system and its functionOperating system and its function
Operating system and its function
Nikhi Jain
 
Embedded c programming22 for fdp
Embedded c programming22 for fdpEmbedded c programming22 for fdp
Embedded c programming22 for fdp
Pradeep Kumar TS
 
Functions Of Operating Systems
Functions Of Operating SystemsFunctions Of Operating Systems
Functions Of Operating Systems
Akshay Kurup
 

Destacado (15)

An Introduction To Python - Variables, Math
An Introduction To Python - Variables, MathAn Introduction To Python - Variables, Math
An Introduction To Python - Variables, Math
 
Variables 9 cm604.7
Variables 9 cm604.7Variables 9 cm604.7
Variables 9 cm604.7
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
 
Constants and variables in c programming
Constants and variables in c programmingConstants and variables in c programming
Constants and variables in c programming
 
Variable declaration
Variable declarationVariable declaration
Variable declaration
 
Variables_c
Variables_cVariables_c
Variables_c
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
 
supercomputer
supercomputersupercomputer
supercomputer
 
constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in C
 
Chapter1 c programming data types, variables and constants
Chapter1 c programming   data types, variables and constantsChapter1 c programming   data types, variables and constants
Chapter1 c programming data types, variables and constants
 
Array in c language
Array in c languageArray in c language
Array in c language
 
Basic of c &c++
Basic of c &c++Basic of c &c++
Basic of c &c++
 
Operating system and its function
Operating system and its functionOperating system and its function
Operating system and its function
 
Embedded c programming22 for fdp
Embedded c programming22 for fdpEmbedded c programming22 for fdp
Embedded c programming22 for fdp
 
Functions Of Operating Systems
Functions Of Operating SystemsFunctions Of Operating Systems
Functions Of Operating Systems
 

Similar a EXTERN -- wherever u define variables, it will get access to use them

Latest C Interview Questions and Answers
Latest C Interview Questions and AnswersLatest C Interview Questions and Answers
Latest C Interview Questions and Answers
DaisyWatson5
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variables
Saurav Kumar
 

Similar a EXTERN -- wherever u define variables, it will get access to use them (20)

Lecture 13 - Storage Classes
Lecture 13 - Storage ClassesLecture 13 - Storage Classes
Lecture 13 - Storage Classes
 
C notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-semC notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-sem
 
Latest C Interview Questions and Answers
Latest C Interview Questions and AnswersLatest C Interview Questions and Answers
Latest C Interview Questions and Answers
 
SPL 9 | Scope of Variables in C
SPL 9 | Scope of Variables in CSPL 9 | Scope of Variables in C
SPL 9 | Scope of Variables in C
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Storage classes in C
Storage classes in C Storage classes in C
Storage classes in C
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variables
 
Structure of the C Program.docx
Structure of the C Program.docxStructure of the C Program.docx
Structure of the C Program.docx
 
6 preprocessor macro header
6 preprocessor macro header6 preprocessor macro header
6 preprocessor macro header
 
C++ Constructs.pptx
C++ Constructs.pptxC++ Constructs.pptx
C++ Constructs.pptx
 
Unit iii
Unit iiiUnit iii
Unit iii
 
Lecture 14 - Scope Rules
Lecture 14 - Scope RulesLecture 14 - Scope Rules
Lecture 14 - Scope Rules
 
Functions.pptx, programming language in c
Functions.pptx, programming language in cFunctions.pptx, programming language in c
Functions.pptx, programming language in c
 
C structure
C structureC structure
C structure
 
Functions_in_Python.pptx
Functions_in_Python.pptxFunctions_in_Python.pptx
Functions_in_Python.pptx
 
basic program
basic programbasic program
basic program
 
Programming Global variable
Programming Global variableProgramming Global variable
Programming Global variable
 
Data structure week 1
Data structure week 1Data structure week 1
Data structure week 1
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 

Más de Ajay Chimmani

Más de Ajay Chimmani (20)

24 standard interview puzzles - Secret mail puzzle
24 standard interview puzzles - Secret mail puzzle24 standard interview puzzles - Secret mail puzzle
24 standard interview puzzles - Secret mail puzzle
 
24 standard interview puzzles - The pot of beans
24 standard interview puzzles - The pot of beans24 standard interview puzzles - The pot of beans
24 standard interview puzzles - The pot of beans
 
24 standard interview puzzles - How strog is an egg
24 standard interview puzzles - How strog is an egg24 standard interview puzzles - How strog is an egg
24 standard interview puzzles - How strog is an egg
 
24 standard interview puzzles - 4 men in hats
24 standard interview puzzles - 4 men in hats24 standard interview puzzles - 4 men in hats
24 standard interview puzzles - 4 men in hats
 
Aptitude Training - TIME AND DISTANCE 3
Aptitude Training - TIME AND DISTANCE 3Aptitude Training - TIME AND DISTANCE 3
Aptitude Training - TIME AND DISTANCE 3
 
Aptitude Training - PIPES AND CISTERN
Aptitude Training - PIPES AND CISTERNAptitude Training - PIPES AND CISTERN
Aptitude Training - PIPES AND CISTERN
 
Aptitude Training - PROFIT AND LOSS
Aptitude Training - PROFIT AND LOSSAptitude Training - PROFIT AND LOSS
Aptitude Training - PROFIT AND LOSS
 
Aptitude Training - SOLID GEOMETRY 1
Aptitude Training - SOLID GEOMETRY 1Aptitude Training - SOLID GEOMETRY 1
Aptitude Training - SOLID GEOMETRY 1
 
Aptitude Training - SIMPLE AND COMPOUND INTEREST
Aptitude Training - SIMPLE AND COMPOUND INTERESTAptitude Training - SIMPLE AND COMPOUND INTEREST
Aptitude Training - SIMPLE AND COMPOUND INTEREST
 
Aptitude Training - TIME AND DISTANCE 4
Aptitude Training - TIME AND DISTANCE 4Aptitude Training - TIME AND DISTANCE 4
Aptitude Training - TIME AND DISTANCE 4
 
Aptitude Training - TIME AND DISTANCE 1
Aptitude Training - TIME AND DISTANCE 1Aptitude Training - TIME AND DISTANCE 1
Aptitude Training - TIME AND DISTANCE 1
 
Aptitude Training - PROBLEMS ON CUBES
Aptitude Training - PROBLEMS ON CUBESAptitude Training - PROBLEMS ON CUBES
Aptitude Training - PROBLEMS ON CUBES
 
Aptitude Training - RATIO AND PROPORTION 1
Aptitude Training - RATIO AND PROPORTION 1Aptitude Training - RATIO AND PROPORTION 1
Aptitude Training - RATIO AND PROPORTION 1
 
Aptitude Training - PROBABILITY
Aptitude Training - PROBABILITYAptitude Training - PROBABILITY
Aptitude Training - PROBABILITY
 
Aptitude Training - RATIO AND PROPORTION 4
Aptitude Training - RATIO AND PROPORTION 4Aptitude Training - RATIO AND PROPORTION 4
Aptitude Training - RATIO AND PROPORTION 4
 
Aptitude Training - NUMBERS
Aptitude Training - NUMBERSAptitude Training - NUMBERS
Aptitude Training - NUMBERS
 
Aptitude Training - RATIO AND PROPORTION 2
Aptitude Training - RATIO AND PROPORTION 2Aptitude Training - RATIO AND PROPORTION 2
Aptitude Training - RATIO AND PROPORTION 2
 
Aptitude Training - PERMUTATIONS AND COMBINATIONS 2
Aptitude Training - PERMUTATIONS AND COMBINATIONS 2Aptitude Training - PERMUTATIONS AND COMBINATIONS 2
Aptitude Training - PERMUTATIONS AND COMBINATIONS 2
 
Aptitude Training - PERCENTAGE 2
Aptitude Training - PERCENTAGE 2Aptitude Training - PERCENTAGE 2
Aptitude Training - PERCENTAGE 2
 
Aptitude Training - PERCENTAGE 1
Aptitude Training - PERCENTAGE 1Aptitude Training - PERCENTAGE 1
Aptitude Training - PERCENTAGE 1
 

Último

Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
Avinash Rai
 

Último (20)

An Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxAn Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptx
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdfPost Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
 
How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17
 
Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17
 
“O BEIJO” EM ARTE .
“O BEIJO” EM ARTE                       .“O BEIJO” EM ARTE                       .
“O BEIJO” EM ARTE .
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdfTelling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
 
Word Stress rules esl .pptx
Word Stress rules esl               .pptxWord Stress rules esl               .pptx
Word Stress rules esl .pptx
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
 
Morse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxMorse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptx
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. Henry
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 

EXTERN -- wherever u define variables, it will get access to use them

  • 1. Also See more posts : www.comsciguide.blogspot.com Extern : All variables we have seen so far have limited scope (the block in which they are declared) within the program. However, in some applications it may be useful to have data which is accessible from within any other file or which remains in existence for the entire execution of the program. Suppose if a variable is declared in file1 and we want to access the variable in another file2 and file3, then how to access these variables in other files??? Simple, the answer to this is “Extern”. Extern keyword is used for the global variables that are defined in one file and can be used those variables in other files by using extern keyword. extern int a; Let us see an example: file1.c file2.c #include<stdio.h> #include<stdio.h> int main() int a=1; { int main() extern int a; {return 0;} printf("%d",a); return 0; } In this example, the variable ‘a‘ is created and initialized in file2.c and is used in file1.c as external variable. For information on how to compile a program whose source code is split among multiple files, you can simply type the command gcc -o a file1.c file2.c, and run the program with ./a.  When you use extern keyword before the global variable declaration, the compiler understands you want to access a
  • 2. Also See more posts : www.comsciguide.blogspot.com variable being defined in another program or file, and hence not to allocate any memory for this one. Instead, it simply points to the global variable defined in the other file. a) #include <stdio.h> extern int i; //extern variable int main() { printf("%d",i); return 0; } Output: Compilation error, undefined symbol i. b) #include<stdio.h> extern int i; int main() { i = 1; return 0; } This program throws error in compilation. Because ‘i’ is declared but not defined anywhere. Essentially, the ‘I’ isn’t allocated any memory. In (b), the program is trying to change the value to 1 of a variable that doesn’t exist at all.  If the global variables are not initialized then, they are initialized to their default values. Int – 0 , Float – 0.0, Char - 0 , String – null #include <stdio.h> int i; char c; float f; char *str; int main() { printf("%d %d %f %s",c,i,f,str);
  • 3. Also See more posts : www.comsciguide.blogspot.com return 0; } Output: 0 0 0.000000 (null)  If we declare a variable as extern, it means that the definition for that variable is defined somewhere else in the program or in other file. It doesn’t allocate the memory for these variables. It’s just the declaration. So we can declare it as many times as we can. #include <stdio.h> extern int i; //Declaring the variable i. int i=1; //Initializing the variable. extern int i; //Again declaring the variable i. int main() { extern int i; //Again declaring the variable i. printf("%d",i); return 0; } Output: 1  A particular extern variable can be declared many times but we can initialize at only one time. (even outside or inside the function of same program) #include <stdio.h> extern int i; //Declaring the variable int i=25; //Initializing the variable int main() { printf("%d",i); return 0; } int i=20; //Initializing the variable Output: Compilation error: Multiple initialization variable i.
  • 4. Also See more posts : www.comsciguide.blogspot.com  To define a variable i.e. allocate the memory for extern variables it is necessary to initialize the variables. #include <stdio.h> extern int i=1; //extern variable int main() { printf("%d",i); return 0; } Output: 1  We cannot initialize extern variable locally i.e. within any block either at the time of declaration or separately. We can only initialize extern variable globally. a) #include <stdio.h> int main() { extern int i=1; // Try to initialize extern variable // locally. printf("%d",i); return 0; } Output: Compilation error: Cannot initialize extern variable. b) #include <stdio.h> int main() { extern int i; // Declaration of extern variable i. int i=1; // Try to locally initialization of // extern variable i. printf("%d",i); return 0; } Output: Compilation error: Multiple declaration of variable i.
  • 5. Also See more posts : www.comsciguide.blogspot.com For more topics Visit : http://comsciguide.blogspot.com/