External variable

External variable

An external variable (keyword extern for C and C++ programming languages) is a variable declaration keyword used to signify that a global variable has been declared in another file, and is simply used within the file containing the extern keyword. There are two specific meanings to extern, which are dependent on context.

When applied to the "declaration of a variable", extern tells the compiler that said variable will be found in the symbol table of a different translation unit. This is used to expose variables in one implementation file to a different implementation file. The external declaration is not resolved by the compiler, but instead by the linker, meaning that extern is a legitimate mechanism to install modularity at the compiler level.

When applied to the "instantiation of a variable", extern promotes that variable into the symbol table of the translation unit, therefore the symbol may be referenced from other translation units. There must be a single implementation of the variable somewhere, marked extern (typically in the header,) for other translation units to refer to the variable.

Two important uses of externality are common: preventing circular references (in a fashion similar to the section declarations of languages like Pascal) and preventing compiled dependency chains.

External variables may be declared outside any function block in a source code file the same way any other variable is declared: by specifying its type and name. No storage class specifier is used - the position of the declaration within the file indicates external storage class. Memory for such variables is allocated when the program begins execution, and remains allocated until the program terminates. For most C implementations, every byte of memory allocated for an external variable is initialized to zero.

The scope of external variables is global, i.e. the entire source code in the file following the declarations. All functions following the declaration may access the external variable by using its name. However, if a local variable having the same name is declared within a function, references to the name access the local variable cell.

Example ("C")

File 1:

int GlobalVariable; // definition void SomeFunction(void); // implicitly external declaration int main() { GlobalVariable = 1; SomeFunction(); return 0; }

File 2:

extern int GlobalVariable; // external declaration

void SomeFunction(void) { ++GlobalVariable; }

In this example the variable GlobalVariable is "defined" in File 1. In order to utilize the same variable in File 2, it must be "declared" using the extern keyword. Regardless of the number of files, a global variable is only "defined" once, however, it must be "declared" using extern in any file outside of the one containing the definition. Technically, SomeFunction is also external, but in C and C++ all functions are considered external by default and usually do not need to be declared.

External links

* [http://msdn.microsoft.com/en-us/library/2tx32sw2(VS.80).aspx Microsoft C Language Reference: The extern Storage-Class Specifier]


Wikimedia Foundation. 2010.

Игры ⚽ Нужно решить контрольную?

Look at other dictionaries:

  • external variable — išorinis kintamasis statusas T sritis informatika apibrėžtis Kintamasis, aprašytas ↑bloko (programos, funkcijos, procedūros ir pan.) išorėje, t. y. jį gaubiančiame bloke. Išorinio kintamojo reikšmę galima naudoti ir keisti. Per ją galima perduoti …   Enciklopedinis kompiuterijos žodynas

  • Variable externa — Una variable externa es, en los lenguajes de programación C y C++, una variable declarada con la palabra clave extern. Contenido 1 Campo de aplicación 2 Ejemplo (C) 3 Referencias …   Wikipedia Español

  • Variable displacement — is an automobile engine technology that allows the engine displacement to change, usually by deactivating cylinders, for improved fuel economy. The technology is primarily used in large, multi cylinder engines. Many automobile manufacturers have… …   Wikipedia

  • External validity — is the validity of generalized (causal) inferences in scientific studies, usually based on experiments as experimental validity. [ Mitchell, M. Jolley, J. (2001). Research Design Explained (4th Ed) New York:Harcourt.] Inferences about cause… …   Wikipedia

  • Variable Damselfly — Scientific classification Kingdom: Animalia Phylum …   Wikipedia

  • Variable yield — or dial a yield is an option available on most modern nuclear weapons. It allows the operator to specify a weapon s yield, or explosive power, allowing a single design to be used in different situations. For example, the Mod 10 B61 bomb had… …   Wikipedia

  • Variable valve timing — Variable valve timing, or VVT, is a generic term for an automobile piston engine technology. VVT allows the lift or duration or timing (some or all) of the intake or exhaust valves (or both) to be changed while the engine is in operation. Two… …   Wikipedia

  • Variable retention — is a relatively new silvicultural system that follows nature s model by always retaining a significant part of the forest after harvesting. Variable retention harvests serve to promote desired natural regeneration while preserving unique features …   Wikipedia

  • Variable universal life insurance — (often shortened to VUL) is a type of life insurance that builds a cash value. In a VUL, the cash value can be invested in a wide variety of separate accounts, similar to mutual funds, and the choice of which of the available separate accounts to …   Wikipedia

  • Variable data printing — (VDP) (also known as variable information printing (VIP) or VI) is a form of on demand printing in which elements such as text, graphics and images may be changed from one printed piece to the next, without stopping or slowing down the printing… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”