+ 1

#include <iostream> using namespace std; int main() { int a,b,c; cin>>a>>b; c=a+b; c=a-b; cout<<c; return 0; }

i want to know why the code didn't showed error when i gave 2 operation to c. and moreover the output of this code is -1 when a=1,b=2, why is it subtract and not adding

30th May 2018, 8:21 AM
Bunny
Bunny - avatar
7 Respuestas
+ 3
Gaganrajdeep Singh You are assigning value to c two time. c=a+b store the addition value. c=a-b overwrite the previous addition value to subtraction of a-b to get different results declare another variable to store the result of a-b.
30th May 2018, 8:44 AM
Zohaib 👑
Zohaib 👑 - avatar
+ 2
COMPILER WILL GO THROUGH YOUR CODE AND IF THERE ARE MULTIPLE ASSIGNMENT TO SINGLE VARIABLE THAN IT WILL CONSIDER LAST ASSIGNMENT. IF YOUR CODE IS a=4; a=5; THAN VALUE OF a WILL BE 5
30th May 2018, 8:48 AM
Meet Mehta
Meet Mehta - avatar
+ 1
You can give variable as many operations as you want(but the variables are Not constant.) And c is assigned "a - b" after assigned "a + b" so this program shows us the result of "a - b".
30th May 2018, 8:47 AM
Disvolviĝo;
Disvolviĝo; - avatar
0
cout << c; add this line after the addition and subtraction you will get different results..
30th May 2018, 8:48 AM
Zohaib 👑
Zohaib 👑 - avatar
0
thnx everyone i got it
30th May 2018, 10:24 AM
Bunny
Bunny - avatar
0
the result will be a-b. Because, c will be a+b. But, then it changes to a-b.
30th May 2018, 12:46 PM
Asalbanu Alimbaeva
Asalbanu Alimbaeva - avatar
0
#include<iostream> using namespace std; int main () { int a, b=3; a=b; cout<<a; }
19th Jun 2021, 4:03 PM
abdulkadir abdulhadi
abdulkadir abdulhadi - avatar