Click here to Skip to main content
15,891,874 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is this related to storing the static in data section or is it compiler dependent??
Posted

Assuming you mean "variable" not "class" as C doesn;t have any concept of classes and all your questions a tagged as (and appear to related to) C rather than C++ or C#!

Compiler dependant, mostly.
In C, static has two uses, depending on where the variable is declared.
1) Outside a function. In this case, all is does is say "this variable is only accessible within this file: nothing outside the file can see it". Which means that you can have two variables with the same name in different files, and they won't interfere with each other.
2) Inside a function. In this case, it says that no matter how many times the function is called, it always gets the same instance of the variable: which means that if your function changes the value, the next time it is called it will have the new value. You need to be very careful here - this is very dangerous in multi threaded applications!

The initial value depends on the compiler, just as it does for all other variables.
 
Share this answer
 
Comments
Andreas Gieriet 14-Oct-15 6:00am    
I agree with all except the "compiler dependent" part. It is not compiler dependent.
See the C Standard.

Static storage duration

Section 5.1.2, paragraph 1: static storage duration: objects are set to their initial values before program startup.

Section 6.2.4, paragraph 3: static storage duration: those objects with internal or external linkage or those declared static.

Section 6.7.8, paragraph 10: objects with static storage duration: implicit initialization to zero/null if not initialized explicitly.

Automatic storage duration

Section 6.2.4, paragraph 4: automatic storage duration: all objects with no linkage and not declared static.

Section 6.7.8, paragraph 10: objects with automatic storage duration: initial value is indeterminate unless explicitly initialized.

Regards
Andi
Maciej Los 14-Oct-15 17:24pm    
Exactly! A 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900