answersLogoWhite

0


Best Answer

Local variables:

These variables only exist inside the specific function that creates them. They are unknown to other functions and to the main program. As such, they are normally implemented using a stack. Local variables cease to exist once the function that created them is completed. They are recreated each time a function is executed or called.

Global variables:

These variables can be accessed (ie known) by any function comprising the program. They are implemented by associating memory locations with variable names. They do not get recreated if the function is recalled.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

Global variables are allocated in the program's data segment and therefore exist in memory for the entire duration the program is running. Local variables are allocated on the call stack and therefore only exist in memory while the variable is in scope.

Global variables are frowned upon because there's no way to limit their scope. Any code can gain access to a global variable, but only local code can gain access to a local variable. Many programmers go out of their way to avoid globals, however if a concept is truly global then it makes sense to use a global. The problem is that far too many programmers use globals for concepts that are categorically non-global.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

In programming, local variables are those that are only accessible within a particular (not global) scope, while global variables are those accessible from anywhere within the same program.

In the C Programming language, global variables are all those declared at a file (module) level but not constraint with the static keyword.

Variables declared at a file level but constraint by the static keyword are sometimes called file-global and sometimes called file-local variables.

In the C programming language, all other variable declarations occur either within a function argument list declaration, or within a pair of curly braces. The function, and the pair of curly braces, defines the scope of the variable definition.

As each variable must have a unique name within its scope, global variables' names must be unique within the entire program, while other variables' names must be unique within their declaration scope (the file, the function, the pair of curly braces).

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are local variables and global variables?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What are the differences between global variables and local variables in C plus plus?

Global variables can be seen in all blocks of your program, when local variables are visible only within the block where it's declared.


Are C variables initialized to 0 by default?

Only global/static variables are, local variables aren't.


How do you hide local variables in c and give precedence to global variables inside the function?

Hi, I would like to answr the question.So, if you want the to give more precedence to global variables with respect to a local one.Just add a pair of curly braces in the local variable and by doing so u can access global variable.


whose variables are also known as global variables?

The variables which are declared outside the main() function is known as global variables and they can be used anywhere in the program. And, the variables which used declare inside the main() function is known as local variables and they can be used inside the main() function only. Example: #include<stdio.h> #include<conio.h> int x,y; // global variables void main() { int a,b; // Local variables ------------ ---------------------- --------------------- getch(); }


If most variables are local in a program instead of global the result is a program with low or high coupling?

Coupling is the interdependency of a program. A program that uses local variables is more independent than one that uses global variables. Therefore, the program would be considered to have lower coupling.


When is better to use global variables rather than utilizing local variables in a program?

In my opinion it is rarely a good idea to use global variables, unless you need to refer to them across modules, or their values need to be keep for a long period of program execution. Local variables should always be used when their lifetime is short, usually only in the module they are declared in. Global variables lifetime will be for the length of the program execution.


What is the difference between a static variable a global variable and a local variable?

A static variable is a variable allocated in static storage. A local variable is a variable declared inside a function. A global variable is a variable declared outside of any class or function. Note that local variables and global variables can both be allocated in static storage.


Is declaration of variables allocated memory?

Constants, static variables and global variables are allocated in the program's data segment at compile time. Local variables are allocated on the stack at runtime. Variables cannot be allocated on the heap, you must use a constant, static variable, global variable or local variable to store the start address of a dynamic memory allocation. The variable must be a raw pointer or a reference handle (a smart pointer).


Why there are no global variables in java?

Global variables are globally accessible. Java does not support globally accessible variables due to following reasons:The global variables breaks the referential transparencyGlobal variables creates collisions in namespac


What is the other name of global variables?

Another name of global variable is "EXTERNAL VARIABLES".


What are global variable?

Variables that are declared globally outside every program are called global variables.


Data values can be referenced as?

INFO-101 Data values can be referenced: Answeras variables local to a routine as a variable in another control as variables global to a routine all of the above