extern Keyword Explanation in C-programming
Program #1 to #14 explain the extern keyword.
#1
main()
{
int n; // Create memory inside main() of size 2 Byte with memory name ‘n’ and memory address 1000 (say), It’s uninitialized local variable initially in memory location 1000 contain Garbage Value.
}
#2
main()
{
extern int n; // local variable sharing Global Variable (with same name ‘n’) value, i.e. don’t create memory inside main() [ It just take Global (Outside) function memory…take this point later ]
}
Explanation for above:
#3
int n = 10; // It’s Global
main()
{
extern int n;
printf(n);// variable n checks Is there any Global Variable present with same name.
Yes “int n = 10” present Global(Outside)
So, print that Global Value i.e. print 10.
}
Output : 10
#4
main()
{
extern int n;
printf(n); );// variable n checks Is there any Global Variable present with same name.
No Variable ‘n’ not present at Global(Outside)
So, it give error Undefined symbol ‘n’
}
Output : error
#5
extern int n;
main()
{
extern int n;
printf(n); );// variable n checks Is there any Global Variable present with same name.
Yes Variable ‘n’ present at Global(Outside) but its value not define
So, it give error Undefined symbol ‘n’
}
Output : error
#6
int n =10;
extern int n;// it will not give error because it not create memory
main()
{
extern int n;
printf(n); // variable n checks Is there any Global Variable present with same name.
Yes Variable ‘n’ present at Global(Outside), and value define in “int n =10”
So, print that Global Value i.e. print 10.
}
Output : 10
#7
int n =10;
main()
{
extern int n;
printf(n); // output : 10
A();
printf(n); // output : 10
B();
printf(); // output : 40 , [B() update the Global Variable n]
}
A()
{
int n =10;
printf(n); // output : 30
}
B()
{
extern int n;
printf(n); // variable n checks Is there any Global Variable present with same name.
Yes “int n = 10” present Global(Outside)
So, print that Global Value i.e. print 10.
n = 40; // update that Global Value
}
#8
main()
{
extern int n;// not create memory inside main and also it is not going to access anywhere in main() body, So no error
printf(“Hello”);
}
Output : Hello
#9
Main()
{
extern int n;
printf(“Hello”); // output : Hello
printf(n); // output : undefined symbol
}
Final Output : error
#10
int n =10
main()
{
int n;// Create memory inside main() of size 2 Byte with memory name ‘n’ and memory address 1000 (say), It’s uninitialized local variable initially in memory location 1000 contain Garbage Value
int n;// already local variable with memory name ‘n’ present in main(), So error
printf(“Hello”)
}
Output : error
#11
int n =10
main()
{
extern int n;
extern int n;
printf(“Hello”)
}
Output : Hello //[writing more than one time “extern int n” not give any error because its not create memory inside main()]
#12
extern int n =10;// If extern is initialize at Global place then it’s same as “int n =10”
main()
{
extern int n;
printf(n);// variable n checks Is there any Global Variable present with same name.
Yes Variable ‘n’ present at Global(Outside), and value define in “extern int n =10”
So, print that Global Value i.e. print 10.
}
Output: 10
#13
Let inside somefile.h define
somefile.h
{
int a =10;
}
#include<somefile.h>
main()
{
printf(n);// variable n checks Is there any Global Variable or Outside function present with same name.
No Variable ‘n’ not present at Global(Outside) function
So, it give error Undefined symbol ‘n’
}
Output : error
#14
Let inside somefile.h define
somefile.h
{
int n =10;
}
#include<somefile.h>
main()
{
printf(n);// variable n checks Is there any Global Variable or Outside function present with same name.
Yes Variable ‘n’ present at Global(Outside) function
So, it give output 10
}
Output : 10 //[i.e. extern also take value from header file]
#1
main()
{
int n; // Create memory inside main() of size 2 Byte with memory name ‘n’ and memory address 1000 (say), It’s uninitialized local variable initially in memory location 1000 contain Garbage Value.
}
#2
main()
{
extern int n; // local variable sharing Global Variable (with same name ‘n’) value, i.e. don’t create memory inside main() [ It just take Global (Outside) function memory…take this point later ]
}
Explanation for above:
#3
int n = 10; // It’s Global
main()
{
extern int n;
printf(n);// variable n checks Is there any Global Variable present with same name.
Yes “int n = 10” present Global(Outside)
So, print that Global Value i.e. print 10.
}
Output : 10
#4
main()
{
extern int n;
printf(n); );// variable n checks Is there any Global Variable present with same name.
No Variable ‘n’ not present at Global(Outside)
So, it give error Undefined symbol ‘n’
}
Output : error
#5
extern int n;
main()
{
extern int n;
printf(n); );// variable n checks Is there any Global Variable present with same name.
Yes Variable ‘n’ present at Global(Outside) but its value not define
So, it give error Undefined symbol ‘n’
}
Output : error
#6
int n =10;
extern int n;// it will not give error because it not create memory
main()
{
extern int n;
printf(n); // variable n checks Is there any Global Variable present with same name.
Yes Variable ‘n’ present at Global(Outside), and value define in “int n =10”
So, print that Global Value i.e. print 10.
}
Output : 10
#7
int n =10;
main()
{
extern int n;
printf(n); // output : 10
A();
printf(n); // output : 10
B();
printf(); // output : 40 , [B() update the Global Variable n]
}
A()
{
int n =10;
printf(n); // output : 30
}
B()
{
extern int n;
printf(n); // variable n checks Is there any Global Variable present with same name.
Yes “int n = 10” present Global(Outside)
So, print that Global Value i.e. print 10.
n = 40; // update that Global Value
}
#8
main()
{
extern int n;// not create memory inside main and also it is not going to access anywhere in main() body, So no error
printf(“Hello”);
}
Output : Hello
#9
Main()
{
extern int n;
printf(“Hello”); // output : Hello
printf(n); // output : undefined symbol
}
Final Output : error
#10
int n =10
main()
{
int n;// Create memory inside main() of size 2 Byte with memory name ‘n’ and memory address 1000 (say), It’s uninitialized local variable initially in memory location 1000 contain Garbage Value
int n;// already local variable with memory name ‘n’ present in main(), So error
printf(“Hello”)
}
Output : error
#11
int n =10
main()
{
extern int n;
extern int n;
printf(“Hello”)
}
Output : Hello //[writing more than one time “extern int n” not give any error because its not create memory inside main()]
#12
extern int n =10;// If extern is initialize at Global place then it’s same as “int n =10”
main()
{
extern int n;
printf(n);// variable n checks Is there any Global Variable present with same name.
Yes Variable ‘n’ present at Global(Outside), and value define in “extern int n =10”
So, print that Global Value i.e. print 10.
}
Output: 10
#13
Let inside somefile.h define
somefile.h
{
int a =10;
}
#include<somefile.h>
main()
{
printf(n);// variable n checks Is there any Global Variable or Outside function present with same name.
No Variable ‘n’ not present at Global(Outside) function
So, it give error Undefined symbol ‘n’
}
Output : error
#14
Let inside somefile.h define
somefile.h
{
int n =10;
}
#include<somefile.h>
main()
{
printf(n);// variable n checks Is there any Global Variable or Outside function present with same name.
Yes Variable ‘n’ present at Global(Outside) function
So, it give output 10
}
Output : 10 //[i.e. extern also take value from header file]
Comments
Post a Comment