Default Values for Pure C data types?
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
Quote:Original post by NerdInHisShoeOr to put it another way; no they're not initialised to any known value. You need to explicitly assign them a value.
They are initialized to some undefined value.
Quote:Original post by Evil SteveQuote:Original post by NerdInHisShoeOr to put it another way; no they're not initialised to any known value. You need to explicitly assign them a value.
They are initialized to some undefined 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
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.
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 sheep19Ah yes, sorry - as you both pointed out, globals and statics are initialised to all binary 0's - stack variables are uninitialised.
static variables are initialized to 0 and static pointers to null.
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.)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement