views:

238

answers:

4

Hi, If I have a global static variable x like in this code

 #include <stdio.h>
 #include <stdio.h>

 static int x;

 int main(void)
 {

 DO SOMETHING WITH x HERE

 x++;

 }

What will be difference if I opted to initialize x to a value first say as in

static int x = 0;  

before entering "main"?

In my first case where I didn't assign a value to x, does the compiler implicitly know that x is to be set to zero as it's a static variable? I heard that we can do this with static variables.

Thanks a lot...

+6  A: 
KennyTM
Thanks a lot for confirming this.
yCalleecharan
Either that or 0.0 if they're floats or NULL if they're pointers. (They all have the same basic value in memory.)
Donal Fellows
You might consider noting that The C99 standard specifies this, someone finding this answer might still be in a position where they have to work around old compilers.
Tim Post
@Tim Post: C90 also specifies this initialization behavior of statically allocated variables, however I have heard that there are a few older compilers that don't follow this rule.
Michael Burr
@Michael Burr: I can confirm that a C compiler I work with every day does not follow that rule... Hopefully something that will be fixed in a future release.
tomlogic
@Michael Burr - Its likely a moot point, I can't find any reference of (at least gcc) not doing it. IIRC, Turbo C 3x was problematic in that regard, but I no longer have a copy of it to try.
Tim Post
@tomlogic: If the vendor hasn't fixed it by now, I'd hold pretty little hope... But, I see from a question that you posted that you're already enhancing the runtime library of your compiler - fixing the static initialization bug yourself might not be too hard. Usually it's just a small loop that zeros out a particular segment/block (or set of segments) early enough in the runtime's initialization. It might have to be in assembler, but the only close to tricky parts are identifying the range(s) to clear and getting it to run at the proper time.
Michael Burr
+1  A: 

Static variables are always implicitly initialized to zero, so there would be no difference in explicitly initializing x to zero.

Lance Richardson
Thanks. Yes I understand.
yCalleecharan
+3  A: 

static variables are automatically initialised to zero (i.e. as though you had assigned zero to them, causing floats and pointers to become 0.0 and NULL, respectively, even if the internal representation of those values is not all bits zero).

Arkku
Thanks. What do you mean by "even if the internal representation of those values is not all bits zero" ? Can you please clarify?
yCalleecharan
@yCallee: If some architecture's NULL is at 0x1000, then the pointer will be initialized to 0x1000 instead of 0.
KennyTM
@yCalleecharan: The C *compiler* treats assigning an integer constant with the value of zero to a pointer as creating a null pointer, but the actual null pointer value for that machine may not be zero. So the pointer may not actually be initialised to all bits zero in memory *on a given machine* but rather as though you had written `= 0`.
Arkku
It's a bit advanced but I'm understanding :). Thanks.
yCalleecharan
+1  A: 

There is a nice answer here:

Just a short excerpt:

First of all in ISO C (ANSI C), all static and global variables must be initialized before the program starts. If the programmer didn't do this explicitly, then the compiler must set them to zero. If the compiler doesn't do this, it doesn't follow ISO C. Exactly how the variables are initialized is however unspecified by the standard.

Smalcat
Thanks for the link. From there I read "The only solution to these problems is to write the code so that all initialization of statics/globals is done in runtime, just before the variable is used". Back to my case, then I can have the line "static int x" inside my "main" just before x is being used? Is this better programming practice?
yCalleecharan
@yCalleecharan: if x is only used by main, then you should declare it inside of main. If you declare it auto, it will always be on the stack. If you declare it static, it won't waste any stack space.
tomlogic
This is very interesting to know. I would be interested to know which books in C discuss such issues. Thanks.
yCalleecharan