+ 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
7 Réponses
+ 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.
+ 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
+ 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".
0
cout << c; add this line after the addition and subtraction you will get different results..
0
thnx everyone i got it
0
the result will be a-b. Because, c will be a+b. But, then it changes to a-b.
0
#include<iostream>
using namespace std;
int main ()
{
int a, b=3;
a=b;
cout<<a;
}