Convert string to int | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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