5

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...

4 Answers 4

8

Static variables with explicit initialization are always initialized to zero (or null-pointer, depending on the type). The C standard §6.7.8/10 has description on this. But explicitly setting it to 0 can help others no need to wonder about the same question :).

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks a lot for confirming this.
Either that or 0.0 if they're floats or NULL if they're pointers. (They all have the same basic value in memory.)
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: 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: 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.
|
5

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.

3 Comments

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: 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.
This is very interesting to know. I would be interested to know which books in C discuss such issues. Thanks.
3

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).

4 Comments

Thanks. What do you mean by "even if the internal representation of those values is not all bits zero" ? Can you please clarify?
@yCallee: If some architecture's NULL is at 0x1000, then the pointer will be initialized to 0x1000 instead of 0.
@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.
It's a bit advanced but I'm understanding :). Thanks.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.