+ 2
Why did that result come out? Answer is <2>
int a; char c='a'; c+=3; c-='b'; a=c; cout<<a;
8 Réponses
+ 4
Output will be 2
In this program you defined (a )which is int type but in char u wrote char c='a'
After that c+=3 so it will calculate ( c=c+3 here c is a which ASCII value is 97 )so it will be 97+3 which is 100 and it will be assign to c variable which type is char
Then in next line ymu written c-='b' which means c=c-'b'
c=100-98 (bcz c was 100 and ASCII of character b is 98 )
So it will give 2 which will be assign to c after that a=c value of 2 will be assign to a variable cout<<a; which will print value of a so output will be 2
+ 3
int a; // declares an integer a
char c='a'; // char c is declared and initialized to 'a'
c+=3; // char c increments by three letters, so c becomes 'd'
c-='b'; // char c is assigned the value 'd'-'b', which is like 4-2=2
a=c; // c is assigned to a, but since a is an int, a='b' gets converted to a=2
cout<<a; // outputs 2
Hope this helps.
+ 3
Considering character 'a' ASCII value is 97
char c = 'a'; // <c> = 'a' (ASCII 97)
c += 3; // <c> is now 'd' (ASCII 100)
c -= 'b'; // 'b' ASCII value is 98
// The above is just 'd' - 'b' pretty much like
// 100 - 98, which gives us -> 2
a = c; // assign value of <c> (2)
// into variable <a>
+ 2
Vladimir
https://code.sololearn.com/chJ7hdrxH87m/#cpp
What I would like to know is why the line 11 outputs a space instead of 'b'. Anyone know?
+ 1
The 'd' character is not present at all )))
0
Because (int = char) use the ASCII code of char. 'a'=97 and 'b'=98 so 97+3=100 and 100-98=2
You can see the ASCII code of character and any key on the keyboard by this code:
#include <conio.h> //the header for getch()
Cout<<getch();
0
Bite of a is :97
c=a=97
97+3=100
Bite of b=98
c=c-b=100-98=2
a=c
a=2