views:

249

answers:

5

When a C program starts and variables are assigned to memory locations, does the C standard say if that value is initialized?

// global variables
int a;
int b = 0;
static int c;

In the above code, 'b' will be initialized to 0. What is the initial value of 'a'? Is 'c' any different since it is static to this module?

+5  A: 

Edit: The following only applies to local variables - not globals.

The initial value of a variable is undefined. In some languages a variable's location in memory is zero'd out upon declaration but in C (and C++ as well) an uninitialized variable will contain whatever garbage that lives at that location at that time.

So the best way to think of it is that an uninitialized variable will most likely contain garbage and have undefined behavior.

Andrew Hare
This is only true for local variables of automatic duration.
Tyler McHenry
-1, it's wrong in the context of global variables. Ping me if the post is changed, then I'll remove the downvote.
Jonas Kölker
Yikes! I didn't notice the "// global variables" comment. I will edit to reflect that.
Andrew Hare
Sadly, I can't remove my downvote. SO says "vote to old to be removed unless post is edited." wtf, it *is* edited...As an addition: the garbage uninitialized auto variables will have is (in most obvious implementation) whatever was stored on the stack.And a piece of pedantry: I think their value is unspecified, not that program behavior is undefined. That is, the variables can hold any value, but the program is not allowed to terminate (or take a wild branch) just because you didn't initialize your variables.
Jonas Kölker
+11  A: 

Since you specifically mention global variables: In the case of global variables, whether they are declared static or not, they will be initialised to 0.

Local variables on the other hand will be undefined (unless they are declared static, in which case they too will be initialised to 0 -- thanks Tyler McHenry). Translated, that means that you can't rely on them containing any particular thing -- they will just contain whatever random garbage was already in memory at that location, which could be different from run to run.

j_random_hacker
Note that static local variables are also initialized to zero.
Tyler McHenry
A: 

A quick test shows a and c to be 0.

int a;
static int c;
int main() {
    printf("%d %d\n", a, c);
    return 0;
}

The location of a (and c) are determined at compile-time; that is, they're neither put on the stack nor in a memory interval returned by malloc. I think the C standard says they're initialized to 0 in all cases, then.

I'm 99.9% confident about with respect to c, and 98% confident with regard to a. The keyword static, in the context of global variables, really is analogous to private in (say) C++ and Java: it's about visibility, not storage location.

What Andrew Hare says about uninitialized variables is true for data stored on the stack or in malloc'd memory. Not so for statically stored variables.

Jonas Kölker
You're right that the C language standard guarantees this behaviour, but your "test" doesn't really test this I'm afraid -- imagine if C didn't set globals to 0, but it just happened that when you ran your program, the values in memory where a and c live already contained 0.
j_random_hacker
when in debug mode, modern debuggers fill memory with a certain bit pattern, so if you haven'T initialized a variable, you immediately know this. for example, visual studio is doing it this way.
codymanix
@codymanix: Good point. (Minor nit: I believe that (for MSVC++ at least) a debug-compiled .EXE initialises memory with that bit pattern itself, i.e. regardless of whether an external debugger is running.)
j_random_hacker
+4  A: 

a will be zero, and c too, if they are global and not explicitly initialized. This is also true for local static variables.

Only local non-static variables are not initialized. Also memory allocated with malloc is not initialized.

See here for rules of initializations and allocation in C for the different objects.

Johannes Schaub - litb
+1 link to other stuff
Tom
+1  A: 

I'm typing way too slowly this morning. Three people popped in quickly as I was answering, so I've removed most of my post. The link I found was clear and brief, so I'm posting that anyhow: Wikipedia on "uninitialized variable" for a discussion of the basic issues.

Telemachus