answersLogoWhite
Computer Programming
Statistics
C Programming

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

495051

Top Answer
User Avatar
Wiki User
2006-10-14 07:50:11
2006-10-14 07:50:11

From Java point of view, static global class members are shared among all class instances and can be accessed from any class member [Methods]. While local global [attributes] can be accessed from any class member [method] but not shared with other class instances The global variable can be shared across multiple files. But the global static variable cannot be shared across multiple files. Hence if you want the variable should be modified in some other file, you can make it a global variable and if you want a global variable only in the file you are declaring and not to shared across, you make it static global. the following code snippet should be useful. //ext2.c int a=10; static int s_b=89; //ext.c extern int a; extern int s_b; int main() { printf("a = %d\n",a); printf("static b = %d\n",s_b); return 0; } then build the exe with ext.o and ext2.o and try running.. you will encounter an error like this "./ext.o(.text+0x20): undefined reference to `s_b'" Regards, ~Naren

1

Related Questions

User Avatar

static variable is available to the module where it is defined. Global variable is available through out the pgm

User Avatar

When you say static variable, do you mean local static variable or global static variable? In C, the difference between global static variables and global variables is that static in this case means that the variable can be used only in the module (.c file) that it is declared. The difference between a local static variable and a global variable is the scope: the local variable can be used only inside the function that declares it. So you can have 2 local static variables in the same file with the same name but in different functions and they will be 2 different variables.

User Avatar

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.

User Avatar

In C programming, a static variable declared outside of any function is global, but local to the current module. A static variable declared within a function retains its value between function calls.

User Avatar

difference between constant and static variables in java

User Avatar

Global (file scope) variable and static global variables both retain their value for the duration of the program's execution. Static global variables are visible only to functions within the file they are declared, while global variables are visible to all compilation units (files) within the linked load module.

User Avatar

extern variables are defined in different than the file where the main() is. extern variables can be accessed from other files. Static global variables are visible only within the file.

User Avatar

A variable declared static outside of a function has a scope the of the source file only. It is not a global variable. A variable declared outside of a function and without the static qualifier would be a global variable.

User Avatar

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.

User Avatar

A static variable has a value that persists throughout the entire execution of the program. A global variable is declared at file scope, i.e. outside of any functions, and therefore has visibility (scope) from all functions, including functions in other compilation units that are linked together. A global variable also has value that persists throughout the entire execution of the program. Do not confuse this with static global, which means that visibility is limited to function in the same compilation unit.

User Avatar

Static VariableA variable that exists in only one location and is globally accessible by all instances of a class and also a variable for which memory remains allocated as long as the program executes.Global VariableA variable that can be accessed by all parts of a program so it does not belong to any subroutine in particular and can therefore can be accessed from any context in a program

User Avatar

They are entirely different things, 'constant' means: not to change its value, 'static' means 'not public' and/or 'not local' variable.

User Avatar

Scope of static variable is with in the file if it is static global. Scope of static variable is with in the function if variable is declared local to a function. But the life time is throughout the program

User Avatar

In C, a variable declared as static in a function is initialised once, and retains its value between function calls. The default initial value of an uninitialized static variable is zero. If a function or global variable is declared static, it can only be accessed in that file.

User Avatar

A static variable is declared once at the start of a program and it does not disappear when it goes out of scope. An auto variable is declared within a function or block and disappears when it goes out of scope.

User Avatar

A local variable is defined and declared within a block. It only has scope (visibility) within that block. Unless it is declared static, it only has lifetime within that block.A global variable is defined and declared at file scope, i.e. outside of any block. It has scope to all blocks within that block. Unless it is declared static, it also has scope to all other modules that are linked to the file. Note that the meaning of static here is entirely different than the meaning of static within a block.

User Avatar

A global variable remains in scope throughout the life-cycle of the program. This exposes the variable to uncontrolled changes from any code that has access to it. A local variable is scoped within a block of code (a function or statement block) and will fall from scope when the code block falls from scope. Thus only the code block can alter the variable. A static local variable is a bit like a global in that it exists throughout the program's life-cycle, however it is still a local variable and therefore has limited scope. The only real difference is that the variable retains its value between calls to the code block, whereas a non-static local variable is destroyed when the code block falls from scope.

User Avatar

Static global variable has scope only in the file in which it is declared. it can't be accessed in any other file but its value remains intact if code is running in some other file means lifetime is in complete programme while static local variable has scope in that function in which it is declared means it can't be used in other functions in the same file also, means scope is limited to the function in which it is declared while its life time is also through out the programme.

User Avatar

There are two ways to declare varibles. 1. Locally 2. Globally When you declare a variable locally in any function that means it is only accessible by that function. When you declare a variable globally so it is accessible by all the functions in the program. Declaring variables with static keyword means you are setting its value null.

User Avatar

there are 3 main uses for the static. 1. If you declare within a function: It retains the value between function calls 2.If it is declared for a function name: By default function is extern..so it will be visible from other files if the function declaration is as static..it is invisible for the outer files 3. Static for global variables: By default we can use the global variables from outside files If it is static global..that variable is limited to with in the file. by mayank shukla there are 3 main uses for the static. 1. If you declare within a function: It retains the value between function calls 2.If it is declared for a function name: By default function is extern..so it will be visible from other files if the function declaration is as static..it is invisible for the outer files 3. Static for global variables: By default we can use the global variables from outside files If it is static global..that variable is limited to with in the file. by mayank shukla

User Avatar

They are unrelated. Static means that there is only one copy of the variable for the entire class - no matter how many objects you have. Final means the value can't change. A variable can be only static, only final, both, or none.

User Avatar

a global variable: a variable defined outside any functions, without keyword static.

User Avatar

static variable also retains its value , at each post back,therby no need to use state management techniques if one is using static variables.

User Avatar

There is no difference between public static void and static public void


Copyright © 2020 Multiply Media, LLC. All Rights Reserved. The material on this site can not be reproduced, distributed, transmitted, cached or otherwise used, except with prior written permission of Multiply.