Difference between static global and global?
Answers were Sorted based on User's Feedback
Answer / rakesh
Static global has file scope and it can't be accessed
outside of the file.
while global has program scope and can be accessed
outside of file in which it has been defined using extern
keyword.
Is This Answer Correct ? | 139 Yes | 7 No |
Answer / sandeep gautham
a program can be spread across two or more files...
so, a global variable can be accessed by all the files..
where as, a static global variable can be accessed only by
the file in which it is declared...static means permanent
and hence the value in static global variable is shared by
all the functions in that file...
Is This Answer Correct ? | 35 Yes | 5 No |
Answer / saugat biswas
I think all the answers above are restricted to C only.
However if extended to C++, static has a file scope and the
static variable is a variable for the class and not the
instances. In other words, static variables are shared
variables. All the instances of the class actualy use the
same static variable. Static variables and function can
directly be called without creating instances of the class
by using scope resolution operator. Static variables are
not initialized in constructors rather they are initialized
as global variables. Example
Example.h
~~~~~~~~~
class A
{
public:
static int x;
A();
~A();
static int getX(void);
}
Example.cpp
~~~~~~~~~~~
int x = 0;
A() //Contructor
{}
~A() //Destructor
{}
int getX(void)
{
return x;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Now in class B we can write:
#include "Example.h"
class B
{
int myX = Example::getX();
}
Is This Answer Correct ? | 18 Yes | 0 No |
Answer / derick
Chitra, "static global is fixed.but global variables are
changed." what does this mean?
Please do not answer like this if you are not sure.
What Rakesh said is true except that Static global
variable's scope is limited to its block.
Is This Answer Correct ? | 21 Yes | 10 No |
Answer / derick
Let us make it clear
int x=22;//global
int* function()
{
static int x = 99; // static x in function
return &x;
}
main()
{
{
//static x in block
static int x = 88;
//returns static in block
printf("\nvalue: %d", x++);
// static x in function
int *p = function();
printf("\nvalue from function: %d", *p);
// change static x in function
*p = 77;
printf("\nvalue from function: %d", *p);
// new value of static x declared in function
}
// returns global x
printf("\nvalue: %d", x);
//still static x declared in function is alive in memory
//but cannot be accessed directly as X since the scope of
//x declared in function is limited to the boundary of
//the function
printf("\nvalue from function: %d", *function());
}
Is This Answer Correct ? | 7 Yes | 4 No |
Answer / veera
hello Sheela u r question is good, but u can access global
static through extern key word in other file,it take the
value but we can,t change the value in that file.
But in case of global variable it's possible to change
the value in other file.
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / ravi.g
local static variable and global variable both are same
since both can be accessed from all the functions in the
same file in which they declared.
Global variable has the file scope since it can be accessed
from other files also by using extern keyword.
Local static vaeriables have no file scope since they can
not be accessed from other files by any means.
Is This Answer Correct ? | 2 Yes | 2 No |
Answer / ravi.g
global static variables have no file scope since they can
not be accessed from other files by using extern keyword
Global variables have the file scope since they can be
accessed from other files using extern keyword
Is This Answer Correct ? | 1 Yes | 1 No |
write a program that takes 5 digit no and calculate 2 power that no and print it.
Difference between strdup and strcpy?
What do you mean by “this” pointer?
What are the total number of lines written by you in C/C++? What is the most complicated or valuable program written in C/C++?
Define virtual constructor.
Explain the volatile and mutable keywords.
Would you rather wait for quicksort, linear search, or bubble sort on a 200000 element array? (Or go to lunch...) a) Quicksort b) Linear Search c) Bubble Sort
Floating point representation and output seems to be compiler dependent?
Why is c++ awesome?
What is linked list in c++?
What is an orthogonal base class in c++?
What is the return value of the insertion operator?