toupper() 函数在 ctype.h 头文件中定义。它有助于将给定的小写字符转换为大写字符。
int toupper(int argument); #where argument will be a character
toupper() 函数接受一个参数,其形式为整数。当传递一个字符时,它会转换为与其 ASCII 值对应的整数值。
| 参数 | 描述 | 必需/可选 |
|---|---|---|
| 参数 | 需要转换的字符 | 必需 |
此函数将给定的小写字符转换为大写字符。该值作为整数值返回,可以隐式转换为 char 类型。如果传递的参数不是小写字母,它将返回相同的字符。
| 输入 | 返回值 |
|---|---|
| 小写字符 | 对应的大写字符 |
| 大写字符或非字母字符 | 字符本身 |
#include <stdio.h>
#include <ctype.h>
int main()
{
char ch, output;
ch = 'A';
output = toupper(ch);
printf("toupper(%c) = %c\n", ch, output);
ch = 'a';
output = toupper(ch);
printf("toupper(%c) = %c\n", ch, output);
ch = '+';
output = toupper(ch);
printf("toupper(%c) = %c\n", ch, output);
return 0;
}
输出
toupper(A) = A toupper(a) = A toupper(+) = +
#include <stdio.h>
#include <ctype.h>
int main () {
int k = 0;
char ch;
char st[] = "programming";
while( st[k]) {
putchar(toupper(st[k]));
k++;
}
return(0);
}
输出
PROGRAMMING