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

What is singleton class in c++?

589


Write a program in c++ to print the numbers from n to n2 except 5 and its multiples

2034


What is abstract class in c++?

579


Why is the function main() special?

619


What are structures and unions?

567






What is else if syntax?

673


What does n mean in c++?

630


What are iterators in c++?

590


Are iterators pointers?

668


What is enum c++?

614


How would you use qsort() function to sort an array of structures?

715


Is c++ the hardest programming language?

634


How did c++ start?

606


Should the this pointer can be used in the constructor?

551


Why isn't sizeof for a struct equal to the sum of sizeof of each member?

538