Amazon Interview Question
Software Engineer / DevelopersThe extern specifier is used when you've declared global variables elsewhere in a separate file for your program. Only global variables can be declared with the extern specifier. When the compiler sees the extern keyword, it'll look in another file for the definition of the variable. This can also be used for function, which we'll see later on.
The static specifier was mentioned in the File Scope section, and can be used on local or global variables to give them permanent duration for that file only. In other words, the memory space reserved for the variable is not erased once the variable is out of its scope.
The register specifier supposedly improves the efficiency of your program. When you declare a variable space in the computer's memory is set aside for it. The computer's central processing unit (CPU) has a few registers which can also take variables in. For the computer, accessing a variable stored in the registers is quicker as opposed to the computer's memory.
The downside is that it is not 100% guaranteed that a variable declared with the register specifier is going to be stored in a register. And you cannot use the "Address of" operator with the variable (like in scanf) because it's not stored in the memory and wouldn't have an address.
So when would you want to use the register specifier? Probably if you have a variable that changes its value often. Like, for example, in a for loop when the value changes after each iteration.
Taken from: http://irc.essex.ac.uk/www.iota-six.co.uk/c/d5_variable_scope.asp
static: 1)if defined in a function,its scope is inside the function and the value can be kept.
- anonymous December 21, 20082)if defined outside any functions ,it is global in the file.
Create when the logic me the variable,destroy when main exit