C++ toupper() 将字符转换为大写
C++ 中的 toupper()
函数用于将给定字符转换为大写。它定义在 cctype 头文件中。
示例
#include <iostream>
#include <cctype>
using namespace std;
int main() {
// 将 'a' 转换为大写
char ch = toupper('a');
cout << ch;
return 0;
}
// 输出:A
toupper() 语法
toupper()
函数的语法是:
toupper(int ch);
toupper() 参数
toupper()
函数接受以下参数:
- ch - 一个字符,转换为
int
类型或EOF
toupper() 返回值
toupper()
函数返回:
- 对于字母 -
ch
的大写版本的 ASCII 码 - 对于非字母 -
ch
的 ASCII 码
toupper() 原型
在 cctype 头文件中定义的 toupper()
函数原型是:
int toupper(int ch);
可以看到,字符参数 ch
被转换为 int
,即其 ASCII 码。
由于返回类型也是 int
,toupper()
返回转换后字符的 ASCII 码。
toupper() 未定义行为
toupper()
的行为在以下情况下是未定义的:
ch
的值无法表示为unsigned char
,或ch
的值不等于EOF