Difference between static global and global?

Answers were Sorted based on User's Feedback



Difference between static global and global?..

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

Difference between static global and global?..

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

Difference between static global and global?..

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

Difference between static global and global?..

Answer / umasankar

what sandeep said is absolutely correct

Is This Answer Correct ?    16 Yes 4 No

Difference between static global and global?..

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

Difference between static global and global?..

Answer / rajesh

also static variables has default value as 0.

Is This Answer Correct ?    6 Yes 2 No

Difference between static global and global?..

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

Difference between static global and global?..

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

Difference between static global and global?..

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

Difference between static global and global?..

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

Post New Answer

More C++ General Interview Questions

Floating point representation and output seems to be compiler dependent?

1 Answers  


Is c++ an integer?

0 Answers  


Define the process of handling in case of destructor failure?

0 Answers  


write a program in c++ to implement stack using functions in header file stack.h

3 Answers   Google, Subex,


How does throwing and catching exceptions differ from using setjmp and longjmp?

1 Answers  






What are the different types of polymorphism?

3 Answers  


What is a tuple c++?

0 Answers  


What is c++ and its features?

0 Answers  


What is the use of 'using' declaration in c++?

0 Answers  


Can non-public members of another instance of the class be retrieved by the method of the same class?

0 Answers  


What is the purpose of ios::basefield in the following statement?

0 Answers  


Discuss the possibilities related to the termination of a program before entering the mainq method?

0 Answers  


Categories