Why does the output of int a = 100; a = 50; cout << a; come out with 50 instead of 100? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why does the output of int a = 100; a = 50; cout << a; come out with 50 instead of 100?

I'm just a little confused

26th Nov 2016, 12:22 AM
Talkiestcobra6
Talkiestcobra6 - avatar
5 Answers
+ 1
because your a = 50 when you declare int a = 100 then a = 100 and then a = 50 then a become 50 that is why a = 50
26th Nov 2016, 12:38 AM
script
+ 1
Because the code executes top-bottom direction, so it first define int a with inicial value of 100, then redefines it with a=50, keeping that last value dor output
26th Nov 2016, 12:46 AM
Aldo Alexandro Vargas Meza
Aldo Alexandro Vargas Meza - avatar
+ 1
You overwrite a variable. Compiler reads one line in time, so first it finds out that a = 100, but then it changed to 50. So, here you are
27th Nov 2016, 9:09 AM
Alex89
Alex89 - avatar
0
thanks. that's been stuck in my head for a while.
26th Nov 2016, 12:42 AM
Talkiestcobra6
Talkiestcobra6 - avatar
0
because first the value is initialized for 'a' but then a new value is assigned to 'a'. Since the print statement is used after the assignment of new value the output comes out to be 50.
26th Nov 2016, 2:22 AM
Arpit saxena
Arpit saxena - avatar