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
Distinguish between a # include and #define.
Do vectors start at 0?
Mention the storage classes in c++.
When must you use a pointer rather than a reference?
How do I get good at c++ programming?
Why do we need pointers?
Where Malloc(), Calloc(), and realloc() does get memory?
What is setbase c++?
Why can’t you call invariants() as the first line of your constructor?
What is difference between c++ 11 and c++ 14?
What is function declaration in c++ with example?
How delete [] is different from delete?
What is null c++?
Does c++ have a hash table?
Why namespace is used in c++?