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 is singleton class in c++?
Write a program in c++ to print the numbers from n to n2 except 5 and its multiples
What is abstract class in c++?
Why is the function main() special?
What are structures and unions?
What is else if syntax?
What does n mean in c++?
What are iterators in c++?
Are iterators pointers?
What is enum c++?
How would you use qsort() function to sort an array of structures?
Is c++ the hardest programming language?
How did c++ start?
Should the this pointer can be used in the constructor?
Why isn't sizeof for a struct equal to the sum of sizeof of each member?