Consider this code:

main()
{
void postinc();
postinc();
postinc();
postinc();
}
void postinc()
{
static int count=1;
printf("%d", count);
count++;
}

What will be the output?
Make me clear of this static concept.........
What happens to the variable count?
I know that it actually retains its value between funcion calls. But when it is called again and again when the control is passed to the function postinc will the stmt "static int count =1" change the previous value contained by count to 1? or will it still hold the value incremented previously. If it is so, what's the reason behind it?
Please reply me soon..................

Recommended Answers

All 2 Replies

Also make me clear about other storage classes, if possible with examples...........

static variables are initialized just once, on the first call to the function.

There was a need for a static variable that would be encapsulated (could be used) by a certain function, and no others.

If you have a particular question about your program's storage classes, post up the code, and ask away.

It doesn't make sense to make a rather long tutorial about a general topic in a forum - it just gets moved off into the archives far too quickly, and then gone.

There are a number of good tutorials available on the net, some are even video's about C, from major colleges and universities. Why not Google them up and view a few?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.