Created
July 31, 2014 23:46
-
-
Save cr1901/389d1b9662b17f978411 to your computer and use it in GitHub Desktop.
Variable Scope Fun
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 <stdlib.h> | |
/* Topics to review: | |
definition vs declaration | |
scope vs storage duration | |
storage classes- auto, static, extern, register | |
internal vs external linkage */ | |
static int my_static = 1; /* This is a definition. This current file and THIS | |
FILE ONLY can access this variable from any location- all variables with the | |
same name refer to the same memory location. */ | |
/* int my_static = 1; Redefinition- illegal */ | |
extern int my_extern; /* This is a declaration. Other files can access this variable- | |
all variables with the same name refer to same memory location. */ | |
/* extern int my_extern = 2; This doesn't make sense, but apparently is legal? */ | |
int my_global = 3; /* This is a definition. This variable is accessible from | |
other files. But variables DEFINED with the same name in other files will error | |
out (multiple definitions). This is what extern DECLARATION is for. It has neither | |
static or extern storage class, but rather static duration, external linkage. | |
See http://stackoverflow.com/questions/4239834/global-variable-in-c-are-static-or-not | |
auto and register are illegal here. */ | |
int main() | |
{ | |
auto int my_local = 4; /* auto is implied FOR LOCALS if no other storage | |
class is used. */ | |
register my_reg_var = 5; /* register- try to place in register. Cannot | |
take address of register. */ | |
printf("We are in main! Here is the value of my_local: %d\n", my_local); | |
printf("Here is the value of my_static: %d\n", my_static); | |
printf("Here is the value of my_extern: %d\n", my_extern); | |
printf("Here is the value of my_global: %d\n", my_global); | |
printf("Here is the value of my_reg_var: %d\n", my_reg_var); | |
{ | |
int my_local = 6; | |
printf("We are now in a new scope!\n"); | |
printf("This is the value of my_local in the new scope: %d\n", my_local); | |
{ | |
extern int my_extern; /* Same my_extern. This is another declaration. */ | |
/* int my_extern; */ | |
my_extern = 7; | |
int yet_another_local = 8; | |
printf("We are in yet another new scope!\n"); | |
printf("Here is the value of yet_another_local: %d\n", yet_another_local); | |
printf("This is the value of my_local in the yet another new scope: %d\n", my_local); | |
printf("This is the value of my_extern in the yet another new scope: %d\n", my_extern); | |
printf("Notice that my_local's value in the previous scope is available!\n"); | |
} | |
} | |
printf("We are now back to the original scope!\n"); | |
printf("We are in main! Here is the value of my_local: %d\n", my_local); | |
printf("Here is the value of my_static: %d\n", my_static); | |
printf("Here is the value of my_extern: %d\n", my_extern); | |
printf("Here is the value of my_global: %d\n", my_global); | |
printf("Here is the value of my_reg_var: %d\n", my_reg_var); | |
return EXIT_SUCCESS; | |
} | |
int my_extern = 2; /* Actual DEFINITION of my_extern. Deliberately putting it here. | |
Notice the source file compiles successfully! Also see: http://stackoverflow.com/a/2652568 */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment