By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,420 Members | 1,133 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,420 IT Pros & Developers. It's quick & easy.

extern vs global

P: 1
Is there any difference between global and extern variables in c?
Mar 12 '07 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 1,510
Is there any difference between global and extern variables in c?
global variables are those defined outside a function body
if declared static they are global to the file otherwise are global to the program, e.g.
Expand|Select|Wrap|Line Numbers
  1. int x=10;       // global to program
  2. static int y;   // global to file from this point onwards
  3.  
  4. int func()
  5. {
  6.    int z;    // local variable to function
  7. ..
  8. }
  9.  
note global variables are initialised to 0 if not explicity initialised (as x was). Local variables are not initialised unless you explicity do so.

to access a global variable in another file you use the keyword extern, e.g.
Expand|Select|Wrap|Line Numbers
  1. extern int x;    // access x defined in another file
  2.  
Mar 12 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.