Thursday, July 9, 2009

What is difference in static member in c,c++?interview question?

In c an static variable unlike auto(default) variable stays in memory and retains its value till program Runs.





whereas In c++ an static variable can be accesses from class resolution without even instantiating any object. Also any number of may be there , but there exists only single copy of static variable in c++ for all class.





for c consider example :





void staticvar()


{


int x=0;


static int y=0;


x++;


y++;


printf("%d %d "x,y);


}


void main()


{


staticvar();


staticvar();


}


output:


1,1


1,2// static retains its value.Auto (x) looses


No comments:

Post a Comment