answersLogoWhite

0

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.

User Avatar

Wiki User

12y ago

What else can I help you with?

Continue Learning about Engineering

How many structure variables of a given type can you use in a C program?

You can use unlimited number of variables for a structure and you can also declare array of structures.


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 a logical use for global variables?

Storing global data.


What is the need of array variables?

congugative memory allocation ,is use to array


When do you declare a variable method and a class final?

When There is No Need to Change the Values of the Variables In Entire lifetime of That variables then we must use that Variable as Final Variable.

Related Questions

Why is external variables in organization not controllable?

check


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 the other name of global variables?

Another name of global variable is "EXTERNAL 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 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.


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.


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

69


Why do you use dummy variables?

to quantify the qualitative variables.


How do you use discouraged in sentence?

James' hurtful comments were discouraging


Which are three of the four main economic variables?

external shocks business investment, and interest rates


How do you use the word discouraged in a sentence one word per sentence please thanks?

The following is a sentence containing the word 'discouraged.' I am so discouraged to see what a poor example was given above. She discouraged her brother from confronting the bully and asked him to report the offense to the school administration instead.


Is it the DoD's stance that the use of social media be discouraged and discontinued?

a.TRUEb.FALSE