dumb question time
I know this is a dumb question but i''m gonna ask it anyway. If I multiplied a variable, let''s call it a and let''s say a = 5. If I put this in my code "a*float." What would that equal. I guess what i''m trying to ask is what is the value of a variable if it''s not initialized to anything?
Thanks, Bino
That would not compile. You cannot say a * float because float is a reserved keyword.
If you just declare a variable but don''t set it to anything it''s usually zero I believe.
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
Depends on the language. I know vb initializes floats, and even arrays of floats to 0.
For C and C++, I think it may depend on the compiler. I''m not sure, but I don''t think an implementation of C has to initialize an uninitialized variable to anything. If this is true, it will just hold the number of the last thing that was stored in that place in memory, either in your program, or a past program. (ie, could be anything...)
correct me if i''m wrong. This should be easy to test.
For C and C++, I think it may depend on the compiler. I''m not sure, but I don''t think an implementation of C has to initialize an uninitialized variable to anything. If this is true, it will just hold the number of the last thing that was stored in that place in memory, either in your program, or a past program. (ie, could be anything...)
correct me if i''m wrong. This should be easy to test.
Yeah I think you''re right, Thrump. I think some compilers won''t initialize the variables to zero.
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
DJGPP only initializes floats to 0, all other types just get some value. I can test it with Borland and MSVC tomorrow, but I guess somebody else will already have the answers by then...
--------------------------Programmers don't byte, they nibble a bit. Unknown Person-------------------------
I haven''t tried myself but several other people said Visual C will initialize variables in debug builds to 0, but not in release builds.
September 05, 2000 03:45 PM
only global variables are supposed to be initialized to zero, it is dangerous to assume otherwise
I''ve never heard of C or C++ initializing its variables (global or otherwise) to some value. I have read that Java does set its variables to 0, but the compiler will give you an error if you use a variable without initializing it yourself. Strange, isn''t it?
The point is, it''s dangerous to assume that the compiler will initialize your variables for you, so I suggest you set them yourself.
Oh, there is one special kind of variable that is always initialized to 0 at startup, and that is a static variable. If it wasn''t, it wouldn''t be of much use (except as global statics). Here''s some code that really needs that feature (although it doesn''t do very much ):
Dormeur
The point is, it''s dangerous to assume that the compiler will initialize your variables for you, so I suggest you set them yourself.
Oh, there is one special kind of variable that is always initialized to 0 at startup, and that is a static variable. If it wasn''t, it wouldn''t be of much use (except as global statics). Here''s some code that really needs that feature (although it doesn''t do very much ):
void func(){ static int count; count++; printf("func was called %d times",count);}
Dormeur
Wout "Dormeur" NeirynckThe Delta Quadrant Development Page
quote: Original post by BitMaster
I haven''t tried myself but several other people said Visual C will initialize variables in debug builds to 0, but not in release builds.
Is this true? It would seem very odd and just point to another Microsoft cock-up if true. How are you supposed to get a correct debug if it initializes to zero AND it could be a valid non-initialize error in your code?
-----------------------------------------------
All messages are of my own personal opinion and
not meant to offend. But if they do - tough :)
Neuro.
-----------------------------------------------All messages are of my own personal opinion and not meant to offend. But if they do - tough :)Neuro.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement