+ 2

Why did that result come out? Answer is <2>

int a; char c='a'; c+=3; c-='b'; a=c; cout<<a;

20th Aug 2020, 6:34 AM
Vladimir Kushner
Vladimir Kushner - avatar
8 Respuestas
+ 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
20th Aug 2020, 8:17 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 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.
20th Aug 2020, 6:44 AM
Edward Finkelstein
Edward Finkelstein - avatar
+ 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>
20th Aug 2020, 7:15 AM
Ipang
+ 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?
20th Aug 2020, 7:39 AM
Edward Finkelstein
Edward Finkelstein - avatar
+ 1
The 'd' character is not present at all )))
20th Aug 2020, 7:14 AM
Vladimir Kushner
Vladimir Kushner - avatar
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();
20th Aug 2020, 4:15 PM
Ali Nadi
Ali Nadi - avatar
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
20th Aug 2020, 9:45 PM
Erfan
Erfan - avatar