Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'static' object in a function [duplicate]

Tags:

c

static

Possible Duplicate:
Does gcc automatically initialize static variables to zero?

Are statically declared objects inside a function guaranteed to be initialized with 0?

For example:

int func(void)
{
   static int x;
   ...
}

Does the standard promise that x = 0 upon the first invocation of func()?

like image 340
Mark Avatar asked May 04 '11 17:05

Mark


2 Answers

The C99 Standard says:

5.1.2 Execution environments

... All objects in static storage shall be initialized (set to their initial values) before program startup.

And it also says that a local variable defined with static qualifier has "static storage" and that in the absence of an initialization all objects take the value 0 of the right type for them.

like image 148
pmg Avatar answered Sep 20 '22 19:09

pmg


That's right. For more insight you can refer to exact same question asked a while ago here:

Does gcc automatically initialize static variables to zero?

like image 35
asami Avatar answered Sep 19 '22 19:09

asami