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 |
What is a pure virtual function? Why is it represented as = 0...how is the internal implementation for the same
Define pre-condition and post-condition to a member function in c++?
Difference between delete and delete[]?
5 Answers Infosys, TCS, Virtusa,
What is class syntax c++?
Do vectors start at 0 c++?
Write a program to reverse a linked list?
8 Answers Catalytic Software, Satyam,
What type of question are asked in GE code writing test based on c++ data structures and pointers?
Differentiate between a template class and class template in c++?
What is the difference between cin.read() and cin.getline()?
Are strings immutable in c++?
Differentiate between declaration and definition.
What is a breakpoint?