Last active
May 12, 2017 15:39
-
-
Save shivabhusal/c3eaa6d6b063da924fa6466db9b37bba to your computer and use it in GitHub Desktop.
Unlike static keyword, extern is used to only declare the variable rather than allocating memory. Just after declaring var, you cannot initialize it, you need to define it as well. In case of function declarations, they are implicitly extern. But, incase of variables, you need to explicitly write extern keyword.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include "../cspec/cspec.c" | |
| // Withour this line, compiler wont compile your code. | |
| // error: `some_var_declared_somewhere` undeclared (first use in this function) | |
| extern int some_var_declared_somewhere; | |
| void test_runner(void); | |
| void main(){ | |
| printf("value of some_var_declared_somewhere is %d", some_var_declared_somewhere); | |
| } | |
| void test_runner(void){ | |
| } | |
| // This var has no visibility in main() function | |
| // because, its defined below main() function; so compiler has no idea | |
| // where and what it is. However, we can tell compiler that the var is | |
| // defined somewhere in some of the source files; dont worry, for now compile the code. | |
| int some_var_declared_somewhere = 10; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment