answersLogoWhite

0


Best Answer

4m Sai. Gloabal Variable is variable which is declared before main (). int a=10; main() { } here a is a global variable. where ever u r using this variable ... we'll get the value as 10. Local Variable is a variable which is declared inside the main(). int a=10; main() { int a=5; ........ ....... } here a=5 is a local variable and a=10 is called as global variable. then if u want to get a value... printf("%d",a); then result 'll be a=5; B'cos compiler gives main preference to local variables.. if any local declareation is not there then it 'll prefer global variable.

User Avatar

Wiki User

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

Wiki User

14y ago

A global variable can be seen in any scope. Local variables can be seen only in their scopes and any sub-scopes. ex:

int x; //global: visible in all scopes

void func()

{ //scope 1

int y; // local: visible in scope 1 and scope 2

{ //scope 2

int z; // local: visible *only* in scope 2

}

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

#include
...
int func_1(...);
...
int var2 = 0;//global variable which is visible for func_1 as well as main
...
int main()
{
...
double var = 0.0;//local variable which is visible only inside of main
...

}

int func_1(...)
{
...int var1 = 0;//local variable which is visible only inside of func_1

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

The global segment is accessible to all code. Local segments are only accessible within the scope of that locality (be it a namespace, file scope, class scope or function scope). Global scope is generally frowned upon in all but the most trivial applications, as it can be difficult to determine when and where alterations to the global segment occur. The global segment should be used solely for global constants whenever possible. Global variables should be replaced with local variables, and all functions that operate upon those variables should be placed within the same scope wherever possible. Where external functions require access to a local variable, passing the variable to the function (by value or by reference) is often the preferred method rather than granting global access.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Local variables are variables that are scoped to a function or code block. Automatic variables are variables that are created for you in the background, either by calling a function with parameters by value, or by evaluating an expression.

void func(int a)

{

// a is a local variable.

// It is not accessible outside of func() unless passed by reference (&a) to another function.

// It will fall from scope when func() returns.

a += 2 * a;

// The expression (2 * a) creates an automatic variable with the value 20.

// The expression (a += 20) creates another automatic variable with the value 30, which is then assigned to a.

return; // a falls from scope at this point.

}

int main()

{

int b = 10;

// b is a local variable.

// b is not accessible outside of main() unless it is passed by reference (&b) to another function.

// It will fall from scope when main() returns.

func( b );

// Pass b by value to func().

// Makes an automatic copy of the value (10).

// Assigns it to the local variable (a) within func().

return( 0 ); // b falls from scope at this point.

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Auto storage is where the compiler generates code to allocate storage on the fly from the stack, within the context of a block. Static storage is where the compiler allocates storage in the module image itself. Static storage persists during the entire run time of the module, which auto storage does not.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

A local object is an object that is defined within a block, i.e. within the scope of at least one pair of braces, { and }. The scope, or visibility, of that local object is constrained to be only the block in which it is defined, and excludes any sub blocks where the same object name is redefined, unless a scope resolution operator is utilized.

A global object is an object that is defined at file scope, i.e. outside of the scope of any block. Scope rules still apply, with the addition that the scope includes other modules, i.e. compilation units, that are linked together, unless that object is declared static, in which case scope only includes that module.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

A local variable is only accessible from within the scope in which it is declared and is destroyed as soon as it falls from scope. A global is accessible from any scope and cannot be destroyed until the program exits.

All variables should be declared local unless a variable represents a truly global concept. That is, if EVERY function requires access to the same variable then a global makes sense. However, be aware that globals are not thread-safe. Given that any function in a process can access a global, it follows that any thread in that process can access the global. This means that globals must use synchronization to ensure only one thread can gain access at any given time. Local variables do not require synchronization.

Note that variables can also be declared static. A static may be global or local, but even when declared local it does not fall from scope (but is not accessible without that scope). This means that its value can be retained across scopes, but it also means that it is not type safe if two threads should concurrently bring same local static variable into scope. Again, synchronisation is the only solution.

Note that scope relates to the point of declaration. A variable declared without any class or function is a global. A variable declared inside a function has function scope. A variable declared inside a class has class scope (is a class member). Non-static class members are known as instance members.

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

They are the same.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between local and global variable of C language?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

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.


What is the difference between a global variable and a private variable?

The accessibility. The global one: almost everywhere in the code may reference to the global variable directly. The private variable, is private to the declaring module (class, method, assembly) only. Outside of that module has no access to it directly.


Difference between global and transnational strategy?

the difference between global and international strategy


Difference between international and global communication?

what is the difference between international communication and global communication


Is a global variable a non-local variable?

True, a variable cannot be both global and local. But if a global and a local variable share the same name, the local one will hide the global.


What is difference between Static and Global variable?

Static may be local of global -local static variable is limited to the function scope. and retain it's value when function is been called . compiler differentiate static variables with a prefix function name while dealing with same name static variable in different functions. - Global static variable is visible to all the function defined in the file and retain it value but it cannot be used outside this file. now Global Variable --- this variable is also visible to all of the functions inside the file but also can be used outside the file via extern keyword.


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.


Difference between global optimization and partial optimization?

essential diffrence between global and local optimization


What is the difference between global depletion of ozone and the hole in the ozone layer?

There is no difference between the two. The global depletion of ozone is the hole over ozone only.


What is the difference between static function and global static function?

'global static'?! There is no such thing.


Is there a difference between international politics and world politics?

what is the difference between global politics and international politics


What is the difference between variable declared inside the declaration part and variable declared in scriptlet part?

Variable declared inside declaration part is treated as a global variable, which means after translation of jsp file into servletthat variable will be declared outside the service method as an instance variablethe scope is available to the complete jspVariable declared inside a scriplet will be declared inside a service method as a local variable and the scope is with in the service method.