且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在char上使用toupper返回char的ascii号,而不是字符?

更新时间:2023-09-29 08:44:46

编写时

std::cout << toupper('a');

发生以下情况:

  1. int toupper(int ch) 会被调用,并返回一个整数,其值为'A'( 0x41 ).
  2. std :: basic_ostream :: operator<<(std::cout,0x41) 被调用,这是 int (2)重载,因为提供了 int .
  1. int toupper(int ch) is called, and returns an integer whose value is 'A' (0x41).
  2. std::basic_ostream::operator<<(std::cout, 0x41) is called, that is the int (2) overload since an int was provided.

总体上,它显示"65".

Overall, it prints "65".

作为解决方案,您可以将大写字母回退为 char :

As a solution, you can cast back your upper case to a char:

std::cout << static_cast<char>(toupper('a'));