The ones who are crazy enough to think they can change the world are the ones who do.- Steve Jobs
extern int m;
Let's workout a program to demonstrate extern storage class in C. First create a file named variable.h where you put all your variables with extern keyword which can be used by any program by simply including the file name in it.
extern int num1 = 9; extern int num2 = 1;
#include <stdio.h> #include "variable.h" int main() { int add = num1 + num2; printf("%d + %d = %d ", num1, num2, add); return 0; }
The program illustrates that an external variables are declared in seperate file with an extension .h which is then including in demanded file.
Though extern varibale is declared above the main function. We can redeclare the extern variable in the main function which will overwrite the global extern variable only within the function.
#include <stdio.h> extern int num1 = 1; extern int num2 = 2; int main() { int num1 = 3; int num2 = 4; int add = num1 + num2; printf("%d + %d = %d ", num1, num2, add); return 0; }
extern variables num1 and num2 are redeclared inside main function.
Keyword | extern |
Storage Type | Memory |
Scope | Block Scope |
Life Time | No validity |
Visibility | Global |
Default Value | nothing (ends in error if value is not initialized) |
Default Storage Class | No |
We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.
© Copyright 2019