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
What are libraries in c++?
Out of fgets() and gets() which function is safe to use and why?
What is a constant reference?
What is the difference between a declaration and a definition?
What apps are written in c++?
Write a program to show polymorphism in C++?
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
Write about the local class and mention its use?
What is nested class in c++?
Which recursive sorting technique always makes recursive calls to sort subarrays that are about half size of the original array?
What is pointer to member?
What is the difference between a definition and a declaration?
What is meant by entry controlled loop?
Explain the properties and principles of oop.
What is encapsulation in C++? Give an example.