Computer Programming
C Programming

What are external variables?

Top Answer
User Avatar
Wiki User
Answered 2007-08-30 10:44:38

The concept of external variable has different meanings from language to language. The primary context in which the phrase is used is in the C and C++ programming languages. The concept is commonly incorrectly explained on the web. Please refer to a real c++ programming text for finality. In C and C++, keyword extern means either of two things, 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. It is important to understand that 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; if, for example, you create a debug and a release version of a library , you can switch between the two simply by loading one or the other, without recompiling either library or the things dependent thereupon. When applied to the instantation of a variable, extern promotes that variable into the symbol table of the translation unit. This is frequently misunderstood to mean that the variable is global; this is incorrect. The variable must already be global for externality to make sense. What this actually means is that 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. Unfortunately, externality is frequently misrepresented as an issue of scope; scope has nothing to do with externality. Scope is an issue of symbol availability and symbol conflict resolution not terribly unlike namespaces. Externs are always global, and thus scope is essentially a foreign concept. Never the twain shall meet. It is incorrect to say that variables in global scope must be externed within the scope of any function that depends upon those globals. 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. In the latter case, consider the example of a video game with a large amount of graphic resources. If the game code is portable, the author cannot rely on compiler extensions like binary inclusion, and therefore must convert their resources to arrays and compile them into the application from source. Because those resources are physically huge, compiling them can take a long time. If these resources are kept each in their own translation unit, however, and referenced as extern from other TUs, then when those other TUs are recompiled, the graphics are just re-linked, meaning they only need to be compiled the once. The effect described above can serve a similarly important role in breaking the recompile chain which otherwise would form from a huge stack of inclusions, by keeping things in separate TUs and therefore modular at the linker level. The more granular such distinctions are, the further compile times drop, and the lower dependencies between modules become; as such, extern is a tremendously important yet rather subtle tool for the modern C/C++ developer.

User Avatar

Your Answer


Still have questions?

Related Questions

What is the other name of global variables?

Another name of global variable is "EXTERNAL VARIABLES".

What is the scope of external variables in C programming language?

Global scope (with external linkage).

Why use of external variables is discouraged?

External variables, or global variables, are generally frowned upon because any code with access to the variables can alter the variables in unexpected ways. Object oriented programming addresses this problem by allowing programmers to encapsulate those variables into an object (often a singleton object) which provides far greater control over how those variables may be altered.

What is the difference between internal and external validity?

The difference between internal and external validity is in their nature. Internal validity indicates if a study depicts relation between two variables. External validity on the other hand generalizes the study of the variables.

What is the difference between internal and external variables?

An internal variable will change due to computations in the program module. An externalvariable will change due to other changes (external input).

What are The Six variables of the external environment that are not directly under the control of marketing managers?


Which are three of the four main economic variables?

external shocks business investment, and interest rates

What is External environment of McDonald's?

The external environment for McDonalds as well as all organizations can include many variables such as: * competition * changing demographics * government interference, laws, regulations * the economy

What is the use of extern variable?

External Variables are declared outside all functions, yet are available to all functions that care to use them. Features of external variables : Storage : Memory Default Initial value : zero Scope : Global Life : As long as program's execution does't come to an end

What is external data type?

whats the difference between external & globle variablesexternal is not a 'data type', it is a 'storage class'.public (=global) means: defined here, accessible from everywhereexternal means: defined elsewhere, accessible from everywherestatic means: defined here, accessible only locally

How do you call an external function or program that is written in a different programming language?

This depends on too many variables you didn't specify for us to answer.

How do you use thehabbos loader what do I put for DCR and External Variables and External Texts?

You don't have to do that. Just go on Prehosted Loader. BTW, (Which means By the way) The loader doesn't work! So I can teach you how to steal a loader.

Internal and external factors effecting consumer decision making in Consumer Behavior?

Internal Variables nPerception nLearning & Memory nMotivation & Values nThe Self nPersonality & Lifestyles nAttitudes nAttitude change & interactive communication External Variables nGroup influence & opinion Leadership nIncome & Social Class nEthnic, Racial and religious subculture nAge subculture

What are the common shell variables in unix?

The common shell variables differ according to which shell you are talking about. In general, they control the shell environment behavior, terminal behavior, and other external things. You can get a list per shell by using the 'man' command with the shell name to list out the common variables used in that shell environment.

What does External Variables mean on habbo retro loader?

It gives information about technical stuff. Without it, you would for instance not be abled to choose clothes for you character.

What are the different storage class in c?

The four storage classes in C are: automatic, static, external and register. Note that storage classes are not classes in the object-oriented programming sense, they simply define the scope (visibility) of a variable.Automatic Variables (auto)All local variables are automatic by default so we seldom see the auto keyword in code. Local variables are variables declared at function scope.Static Variables (static)All global variables are static by default. Global variables are variables declared at file scope (outside of any function). Static variables can also be explicitly declared inside functions to override the default automatic storage class. All static variables, whether global or local, are allocated within the program's data segment (static memory) and do not fall from scope even if declared locally. All static variables are initialised to zero by default.It's best to avoid the use of global variables unless they are declared constant (const) as it can be difficult to keep track of all the places where a global variable is being operated upon (accessed or assigned to). This can lead to data races in multi-threaded applications unless we take steps to synchronise all access and assignment operations upon the variable. For that reason it's best to keep variables as localised as possible, passing arguments into functions whenever we need to cross scopes. Non-constant global variables should really only be considered if they truly represent a global concept within the file in which they are declared.External Variables (extern)External storage can only be applied to a global variable declared outwith file scope. That is, when a global variable is declared in one file, any external file can gain access to that same global variable simply by declaring the same name and type but with external storage. It follows that external variables are also static variables and is the only case where a variable has two storage classes. Note the local static variables (including local constant variables) cannot be declared external, they are local to the function in which they are declared.This is another reason why it is best to avoid using too many global variables. While we can generally keep track of which code can access a global variable at file scope we have no means of limiting access from outwith that file. Again, prefer local variables to global variables whenever possible.Register Variables (register)A register variable is a variable that we wish to allocate to a CPU register rather than in RAM. Register variables must be no larger than the word-length of the machine and should only be used when we explicitly require fast access to the variable, such as loop counters, accumulators and pointer variables. Note that CPU registers have no address (no identity we can refer to) so we cannot use the unary '&' operator to take the address of a register variable. This means we cannot use pointers to refer to them indirectly which, in turn, means we can only pass them to functions by value (not by reference). However, to do so would defeat the purpose of using the register storage class.Given the limited number of registers available, there is no guarantee that a register variable will actually be allocated to a register; the register keyword is merely a hint to the compiler. It should be noted that modern compilers are extremely good at optimising code so there is seldom any need to explicitly declare register variables.

What is the difference between public private and protected access modifiers in C?

We use these keywords to specify access levels for member variables, or for member functions (methods). Public variables, are variables that are visible to all classes. Private variables, are variables that are visible only to the class to which they belong. Protected variables, are variables that are visible only to the class to which they belong, and any subclasses. Deciding when to use private, protected, or public variables is sometimes tricky. You need to think whether or not an external object (or program), actually needs direct access to the information. If you do want other objects to access internal data, but wish to control it, you would make it either private or protected, but provide functions which can manipulate the data in a controlled way

What is the storage allocation and scope of global extern static local and register variables?

Extern is not a storage allocation specifier, it declares external linkage. That is, the declared variable or constant will be defined in a separate translation unit. Variables declared in global scope are allocated in static memory, as are all constants and static local variables. Local (non-static) variables are allocated on the (current) call stack. In multi-threaded applications, each thread has its own call stack. Register variables are automatic variables allocated within a CPU register, however the language compiler ultimately decides when or indeed if this is appropriate.

What is the difference between global variable and extern variable?

Global variables are non-local variables. That is, variables that are not defined in a function or class. They are globally accessible to all code in the same translation unit. External variables are global variables that have external linkage; they are accessible across translation units.

Explain different types of variable?


How many variables are tested during an experiment?

There are three types of variables tested: manipulated variables, controlled variables, and experimental variables.

The difference between the independent and dependent variables?

Independent variables are variables that can be changed in an experiment, while dependent variables are variables that change as a result of an experiment. In other words, independent variables are what you change, and dependent variables are the results of the experiment.

What is the differecnce between independent variables and dependent variables?

Every time the independent variables change, the dependent variables change.Dependent variables cannot change if the independent variables didn't change.

What are the different variables during an experiment?

Independent variablesDependent variables

What are the three variables that play an important role in a Practical Investigation?

Independent Variables, Dependent Variables and Extraneous Variables.