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

Distinguish between a # include and #define.

657


Do vectors start at 0?

599


Mention the storage classes in c++.

658


When must you use a pointer rather than a reference?

610


How do I get good at c++ programming?

608






Why do we need pointers?

589


Where Malloc(), Calloc(), and realloc() does get memory?

617


What is setbase c++?

631


Why can’t you call invariants() as the first line of your constructor?

569


What is difference between c++ 11 and c++ 14?

586


What is function declaration in c++ with example?

552


How delete [] is different from delete?

605


What is null c++?

599


Does c++ have a hash table?

551


Why namespace is used in c++?

613