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 near, far and huge pointers? How many bytes are occupied by them?
When to use “const” reference arguments in a function?
What will happen if a pointer is deleted twice?
What is an operator function? Describe the function of an operator function?
Is c++ an oop?
Does c++ vector allocate memory?
What is flush programming?
Explain the difference between static and dynamic binding of functions?
Assume an array of structure is in order by studentID field of the record, where student IDs go from 101 to 500. Write the most efficient pseudocode algorithm you can to find the record with a specific studentID if every single student ID from 101 to 500 is used and the array has 400 elements. Write the most efficient pseudocode algorithm you can to find a record with a studentID near the end of the IDs, say in the range from 450 to 500, if not every single student ID in the range of 101 to 500 is used and the array size is only 300
what is COPY CONSTRUCTOR and what is it used for?
Is it legal in c++ to overload operator++ so that it decrements a value in your class?
Explain how we implement exception handling in c++?
What is the best c c++ compiler for windows?
What's c++ used for?
Is java based off c++?