+ 1

Why in first case, value of "i" is 8, but in second case, value is 0? I only made a declaration of variable "i"? Does anyone kno

Case 1: #include <iostream> using namespace std; int main() { int x = 2; int i; // i = 0 cout << i << endl; return 0; } #Output: 8 Case 2: #include <iostream> using namespace std; int main() { int x = 2; int y = 10; int i; // i = 0 cout << i << endl; return 0; } #Output: 0

20th Sep 2019, 1:35 PM
James Anderson
James Anderson - avatar
2 ответов
+ 4
If you don't initialize a variable he get a random value.
20th Sep 2019, 1:41 PM
KfirWe
KfirWe - avatar
+ 3
Global variables initialized to 0 by default. Local variables are not initialized. So they will have the garbage value (i. e. Random value), which can not be predicted.
20th Sep 2019, 2:00 PM
Kuri
Kuri - avatar