can someone explain this ? how it works ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

can someone explain this ? how it works ?

#include <iostream> #include <cmath> using namespace std; int main() { int i; for(int i=0;i<0;i++){ } cout<<i; } //output CodeBlocks7208852 //output SoloLearn 8

16th Apr 2019, 8:41 AM
San Anemos
San Anemos - avatar
5 Answers
+ 2
It's a garbage value nothing else int i =0 is clearly out of scope from the outside scope. Whenever you try to print something or do some operation on some variables which are defined but never initialized compiler is free to crash at that moment. In practice, they tend to just have some nonsensical value(garbage) in there initially - some compilers may even put in specific, fixed values to make it obvious when looking in a debugger - but strictly speaking, the compiler is free to do anything from crashing to summoning a pokemon. To avoid getting that 0 just initialize that i with 0 at starting.
16th Apr 2019, 8:59 AM
Yugabdh
Yugabdh - avatar
+ 5
The compiler is free to do anything from crashing to summoning a pokemon - Yugabdh 2019
16th Apr 2019, 2:30 PM
Shahil Ahmed
Shahil Ahmed - avatar
+ 1
Flaming Arrow xD Make it quote Make it viral Put it on fire
16th Apr 2019, 4:34 PM
Yugabdh
Yugabdh - avatar
0
You can even generate the same by int main() { int i; int x= 0; cout<<i; } //output SoloLearn 8 Clearly speaking its a garbage value that's it
16th Apr 2019, 9:00 AM
Yugabdh
Yugabdh - avatar
0
Thanks
16th Apr 2019, 9:01 AM
San Anemos
San Anemos - avatar