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


Please Help Members By Posting Answers For Below Questions

Explain unexpected() function?

580


Is c++ proprietary?

573


Explain the static member function.

699


How do you clear a buffer in c++?

533


How are Structure passing and returning implemented by the compiler?

593






How can you prevent accessing of the private parts of my class by other programmers (violating encapsulation)?

637


Why was c++ made?

649


Which operations are permitted on pointers?

562


What does new return if there is insufficient memory to make your new object?

578


Difference between delete and free.

610


What is a memory leak c++?

585


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?

663


What is pointer -to-members in C++? Give their syntax?

593


Why ctype h is used in c++?

519


Describe the syntax of single inheritance in C++?

641