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
Explain unexpected() function?
Is c++ proprietary?
Explain the static member function.
How do you clear a buffer in c++?
How are Structure passing and returning implemented by the compiler?
How can you prevent accessing of the private parts of my class by other programmers (violating encapsulation)?
Why was c++ made?
Which operations are permitted on pointers?
What does new return if there is insufficient memory to make your new object?
Difference between delete and free.
What is a memory leak c++?
If a round rectangle has straight edges and rounded corners, your roundrect class inherits both from rectangle and from circle, and they in turn both inherit from shape, how many shapes are created when you create a roundrect?
What is pointer -to-members in C++? Give their syntax?
Why ctype h is used in c++?
Describe the syntax of single inheritance in C++?