Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Use of static variable in c?  RSS feed

 
mara thamizhan
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
what is the use of static variable in c?

cheers,
Bye!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"c" as in "C", the language? This here's a Java forum, boy!

But I suppose it won't hurt to answer, as somebody moving from C to Java might benefit.

In "C", a static variable is one that is private to one "translation unit" (one source file.) It won't be accessible from other code. That lets you write little "modules" that consist of some private data and the functions that operate on it.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't C also have static local variables - local variables that are only initialized the first time a function is called, and then remember there value from previous calls?
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Posted by Ilja Preuss:-
Doesn't C also have static local variables
As far as I can remember, yes.
 
Balaji Dadge
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
1)Scope static storage class is only local to the related to piece(May be it function to class(for c++)) but its value does not reinitialised until it modified.
2)Its default value is not garbage but 0.
3)When you call to any main program compiler/system programm always call to static variable before main.
4)As its value does not reinitialised, it is useful for counter purpose.
i.e, how many times any particular function or piece of code is get accessed.

I think it is useful.
 
Balaji Dadge
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
1)Scope static storage class is only local to the related to piece(May be it function to class(for c++)) but its value does not reinitialised until it modified.
2)Its default value is not garbage but 0.
3)When you call to any main program compiler/system programm always call to static variable before main.
4)As its value does not reinitialised, it is useful for counter purpose.
i.e, how many times any particular function or piece of code is get accessed.

I think it is useful.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!