+ 2

Convert string to int

Why it output 50? #include <iostream> using namespace std; int main() { cout << '2' + '2' - '2'; return 0; } // output: 50

20th May 2020, 4:11 PM
Hiáșżu VĂ” Tráș§n Minh
Hiáșżu VĂ” Tráș§n Minh - avatar
3 Answers
+ 12
In this case, the ASCII value of characters were involved in the addition and subtraction operations. http://www.asciitable.com/mobile/ The character '2' has an ASCII value of 50. 50+50-50 is 50.
20th May 2020, 4:15 PM
Hatsy Rei
Hatsy Rei - avatar
+ 7
That is not a string, since it is enclosed in single quote, therefore '2' is a char, and the ASCII value of char '2' is 50.. Therefore the expression is 50 + 50 - 50 and prints 50 as output
20th May 2020, 4:16 PM
Мг. ĐšĐœĐ°ĐżđŸŒ 
Мг. ĐšĐœĐ°ĐżđŸŒ  - avatar
+ 1
Thank you so much.
21st May 2020, 11:56 AM
Hiáșżu VĂ” Tráș§n Minh
Hiáșżu VĂ” Tráș§n Minh - avatar