2013/10/16

"extern" in c programming

Quick and dirty summery
1. extends the visibility for a variable or function
2. While using with variable, it declares a variable but not defining it.
3. Special case for (2) is "extern int bar=0;", it treat as definition


Dig deeper
Declaration := A variable or function signature exists somewhere in the program but the memory is not allocated for them.
Definition := Despite the stuff declaration does, it also allocates memory for that variable/function.

PS.
/* function */
int foo (int arg1, int arg2); /* Declaration */
int foo (int arg1, int arg2){
    /* Definition */
     return 0;
}
/* variable */
extern int bar; /* Declaration */
int bar=0; /* Definition */



Reference:
http://www.geeksforgeeks.org/understanding-extern-keyword-in-c/

沒有留言:

張貼留言