I'm asking you a question about the C++ beginner's ASCII code

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

#include <stdio.h>

int main() {
    printf("%c + %c = %c\n", '2','3', '2' +'3');  //1
    printf("%c + %c = %c\n", '2', '3', '2' + 3); //2

}

In 1, the ASCII value of 2 is 50 and the ASCII value of 3 is 51 so if you add it up, it's 101 and the ASCII character corresponding to 101 is e, so I understand that e came out.

In 2, there was no text corresponding to the Aski value of 53 and just 5 came out, so I'm asking why.

c++

2022-09-20 13:15

1 Answers

The ASCII code value for '2' is 50 and the ASCII code value for '5' is 53.

'2' + 3 will be 55. You used %c in printf() so the character '5' for 55 was printed.

Try the following:

printf("%c + %c = %c, %d\n", '2', '3', '2' + 3, '2' + 3); //2


2022-09-20 13:15

If you have any answers or tips


© 2022 pinfo. All rights reserved.