Default Values for Pure C data types?

Started by
10 comments, last by rip-off 14 years, 8 months ago
Hi Guys, Is there a default value for the built-in data types in Pure C? I know in Java, the default value for integer is 0 and for boolean is false. Do you know if Pure C has the same concept of initial default values? Jr
Advertisement
They are initialized to some undefined value.
Johnny was a chemist's son by Johnny is no more, for what Johnny thought was H2O was HO4
Quote:Original post by NerdInHisShoe
They are initialized to some undefined value.
Or to put it another way; no they're not initialised to any known value. You need to explicitly assign them a value.
Quote:Original post by Evil Steve
Quote:Original post by NerdInHisShoe
They are initialized to some undefined value.
Or to put it another way; no they're not initialised to any known value. You need to explicitly assign them a value.

Are you sure? I think some such as global static variables are default initialised to 0, also there is the case of initialising a container (array or struct).
For example
typedef struct foo{int _1;char _2;};foo f;f = foo{};

_1 and _2 are default initialised.

edit:
As I thought
6.7.8
Quote:
10 If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:
— if it has pointer type, it is initialized to a null pointer;
— if it has arithmetic type, it is initialized to (positive or unsigned) zero;
— if it is an aggregate, every member is initialized (recursively) according to these rules;
— if it is a union, the first named member is initialized (recursively) according to these
rule
static variables are initialized to 0 and static pointers to null.
As the previous posts state : C/C++ variables are not initialized by default. You have to initialize them yourself. When you declare a variable , the compiler places the variable at a memory address. Consequently , the variable will hold whatever is at that particular memory address.
As far as I know , global variables are initialized with 0. Static variables as well.
It is a good practice to initialize variables before you use them.
Quote:Original post by dmail
Are you sure? I think some such as global static variables are default initialised to 0, also there is the case of initialising a container (array or struct).

Quote:Original post by sheep19
static variables are initialized to 0 and static pointers to null.
Ah yes, sorry - as you both pointed out, globals and statics are initialised to all binary 0's - stack variables are uninitialised.
Out of interest, any idea why this is so? Is it simply because the cost of initialising global or static variables (i.e. initialisation one time only) is virtually zero, whereas initialising stack variables to zero would eat up some cycles? If so, why are the global/static variables initialised to zero rather than just treated the same way as stack variables?
Static and global variables are usually implemented by placing their storage in a data segment in the executable. When you load the executable image into memory it basically copies the entire file into memory, which means that initializing static and global variables is actually free since their initial values are set in the file and will get loaded the same way no matter what the initial values are. (This is something of a simplification.)
Guy,

Thanks for all this help. I was just curious in case someone had ask me about initializing built-in types in C. Now you guys have provided good clarification for me.

Thanks,
Jr

This topic is closed to new replies.

Advertisement