Difference between static global and global?

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are libraries in c++?

601


Out of fgets() and gets() which function is safe to use and why?

719


What is a constant reference?

612


What is the difference between a declaration and a definition?

577


What apps are written in c++?

598






Write a program to show polymorphism in C++?

621


In int main(int argc, char *argv[]) what is argv[0] a) The first argument passed into the program b) The program name c) You can't define main like that

611


Write about the local class and mention its use?

601


What is nested class in c++?

512


Which recursive sorting technique always makes recursive calls to sort subarrays that are about half size of the original array?

601


What is pointer to member?

597


What is the difference between a definition and a declaration?

570


What is meant by entry controlled loop?

656


Explain the properties and principles of oop.

524


What is encapsulation in C++? Give an example.

582