The output was 40 how it possible | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

The output was 40 how it possible

#include <iostream> using namespace std; int main() { int x,d,k; d=5; k=x*d; cout << k; }

21st Jul 2019, 11:53 AM
poo malai
poo malai - avatar
4 Answers
+ 6
That's because you are using an uninitialized variable in your expression which is x. x will have some garbage value which is multiplied by d(5) and assigned to the variable k. It's a good practice to initialize variables at the time of their declaration to avoid any logical errors.
21st Jul 2019, 12:03 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 4
No it will be different in every device. What happens is, some program uses memory and when it exits, that memory is freed (available for use by other programs). Now there could be a chance that a variable that you created in your program will point to the part of memory location which was used by the previous program. Now that memory address is holding some garbage value which is also pointed by the same variable you created. So, now that variable has garbage value in it.
21st Jul 2019, 2:37 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 3
nAutAxH AhmAd Does the x takes the value of 8 in any device (ie., does the garbage value of x is the same in any device) coz I get the same result of 40
21st Jul 2019, 12:39 PM
deeyae
deeyae - avatar
0
Anybody please answer me I am very confused
21st Jul 2019, 11:53 AM
poo malai
poo malai - avatar