tags:

views:

184

answers:

2
+2  A: 

static means a variable will be globally known only in this file. extern means a global variable defined in another file will also be known in this file, and is also used for accessing functions defined in other files.

A local variable defined in a function can also be declared as static. This causes the same behaviour as if it was defined as a global variable, but is only visible inside the function. This means you get a local variable whose storage is permanent and thus retain its value between calls to that function.

I'm no C expert so I might be wrong about this, but that's how I've understood static and extern. Hopefully someone more knowledgable will be able to provide you with a better answer.

EDIT: Corrected answer according to comment provided by JeremyP.

gablin
Technically you mean "extern means a global variable *defined* in another file will also be known in this file". In C, *definition* is where an object is 'created' (and optionally initialised). Other than that, you are about on the nail (sweeping variables qualified static in a block under the carpet).
JeremyP
@JeremyP: Ah yes, of course. I meant _defined_, but mixed up the two terms. Thanks for pointing this out (+1); I have updated the answer accordingly, and added the 'thing' about static variables in a function. ^^
gablin
+1  A: 

"The static storage class is used to declare an identifier that is a local variable either to a function or a file and that exists and retains its value after control passes from where it was declared. This storage class has a duration that is permanent. A variable declared of this class retains its value from one call of the function to the next. The scope is local. A variable is known only by the function it is declared within or if declared globally in a file, it is known or seen only by the functions within that file. This storage class guarantees that declaration of the variable also initializes the variable to zero or all bits off.

The extern storage class is used to declare a global variable that will be known to the functions in a file and capable of being known to all functions in a program. This storage class has a duration that is permanent. Any variable of this class retains its value until changed by another assignment. The scope is global. A variable can be known or seen by all functions within a program. ."

taken from : http://wiki.answers.com/Q/What_is_the_difference_between_static_and_extern

Danail