# odd thing

## Recommended Posts

#include <iostream> main() { float d = rand()/RAND_MAX; } It can compile ,why?

##### Share on other sites
You're compiling with optimization flags, that remove redundant code.

##### Share on other sites
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.

##### Share on other sites
No you're right Scet.

##### Share on other sites
Quote:
 Original post by xorYou're compiling with optimization flags, that remove redundant code.

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

##### Share on other sites
*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.

##### Share on other sites
no ,I mean I do not include any head file ,but the code still pass through the compiler.

##### Share on other sites
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?

##### Share on other sites
For the love of God!!!

Quote:
 maybe you wrong,I remove ,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.

##### Share on other sites

#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.

##### Share on other sites
Why do people still responde seriously to these posts? This guy just likes to post junk posts.

##### Share on other sites

#include <iostream>

main()
{

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

}

if I remove iostream ,it can not be compile.maybe because of iostream?

##### Share on other sites
Quote:
Original post by xor
For the love of God!!!

Quote:
 maybe you wrong,I remove ,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.
That can't be it. Without knowing the definition of rand because the necessary header is not included, it cannot know that rand() does not have side-effects. Therefore it can not be optimised out.
And for that matter, it doesn't even know that rand() is a function, and should not be able to compile that code.
Perhaps he messed with the iostream header?
Quote:
 Why do people still respond seriously to these posts? This guy just likes to post junk posts.
Anything that he posts may catch the eyes of some other beginner, who with proper guidance, can learn from his mistakes.

##### Share on other sites
#include <stdlib.h>int main(){   printf("Initial value of RAND_MAX is %d. Considered this rand() give me %d.\n"      "Is obviously that the value divided by %d (converted also to a float) give me a number ~0.\n"	  "I can try to write a little log function like this cause the program is only 2 lines.\n"	  "And there are not many technical difficulties doing this.", RAND_MAX, rand(), RAND_MAX);   return 0;}

##### Share on other sites
There seems to be some kind of mess with iostream there, rand is defined in stdlib.h, maybe it's being included somewhere else.

Anyway iMalc, OP says(i think) that he can compile it when he removes iostream and without cout, so the remaining line is being ignored. There is a rand() there and a definition that aren't included anywhere, and yet it compiles, so they must be ignored. Or maybe some sort of script is running at compile time that's including more stuff, and you're right after all.

Either way, this is boring, it's probrably a PEBKAC error anyway, so i'm off.

[Edited by - xor on June 3, 2005 2:04:45 PM]

##### Share on other sites
Quote:
 Original post by xorThere seems to be some kind of mess with iostream there, rand is defined in stdlib.h, maybe it's being included somewhere else.

If you're using C++, you'd want to use cstdlib instead of stdlib.h . You'd also want to seed rand(). I haven't compiled it myself, but you might want to try the following:

#include <iostream>#include <cstdlib>#include <ctime>int main(){    srand(static_cast<unsigned>(time(0))); // there are many different ways to do this    float d = rand()/RAND_MAX;    std::cout << "RAND_MAX: " << RAND_MAX << std::endl;    std::cout << "d: " << d << std::endl;    std::cin.get();    return 0;}

##### Share on other sites
Why it compiles?

Have a look into the project-properties under c/c++, precompiled headers. If the setting is something like "use precompiled headers" and not "auto generate precompiled headers" than thats why it compiles.

If the headers are not auto-generated, than adding/removing include-files will not affekt the precompiled headers, so, as in this case, the removed include-files are still in the precomiled header and are still beeing used.

Try to clean-compile or set the header option to auto-generate.

Oh, and rtfm...

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628333
• Total Posts
2982130

• 24
• 9
• 9
• 13
• 11