odd thing

Started by
15 comments, last by schue 18 years, 10 months ago
#include <iostream> main() { float d = rand()/RAND_MAX; } It can compile ,why?
Advertisement
You're compiling with optimization flags, that remove redundant code.
Do you mean "can't" compile? If so it's probably because rand is declared in <cstdlib> not <iostream>. Well at least for me it is, but my header versions are rather old.
No you're right Scet.
Quote:Original post by xor
You're compiling with optimization flags, that remove redundant code.


maybe you wrong,I remove <iostream>,so It can still compile. I use vc2003.net
*sigh*

NOT EVERYTHING IS IN IOSTREAM!!!

There's a reason why your include directory has many header files in it. Each of them declares different things.

#include <iostream>#include <cstdlib>int main(){float d = rand()%RAND_MAX;return 0;}


Normally I'm not one to recommend programming books, but since you are incapable of solving the most simple problems and continue spamming the boards(9 topics in 1 day) I suggest you go buy a beginning C++ book or go to your local library.
no ,I mean I do not include any head file ,but the code still pass through the compiler.

Well that sounds strange. Mabye it's because you're using .net, most C++ compilers should bitch at you for not including cstdlib. Anyway if it works what is this thread about?
For the love of God!!!

Quote:maybe you wrong,I remove <iostream>,so It can still compile. I use vc2003.net


You compiling an empty main!
The optimization flag activates a method of optimization that consists of removing redundant code, code that isn't going to be needed is removed. So that line " float d = rand()/RAND_MAX;" is going to be removed, you're compiling an empty main. So it doesn't matter what headers you include or exclude, the thing is allways going to compile, because an empty main needs nothing.

Now if you want to check this, just include a "printf("%f\n");" at the end of that code and it won't compile. Why? Because now the variable 'd' is actually used somewhere, it is not redundant, it will not be removed by the optimization.


#include <iostream>




main()
{

float d = rand()/RAND_MAX;std::cout<< d;


}



no ,take a look this code .it output 0!!!
I debug ,and I find d exist and value is 0.

This topic is closed to new replies.

Advertisement