非常教程

C参考手册

字符串 | Strings

toupper

在头文件<ctype.h>中定义

int toupper(int ch);

根据当前安装的C语言环境定义的字符转换规则,将给定的字符转换为大写字符。

在默认的“C”语言环境中,下面的小写字母abcdefghijklmnopqrstuvwxyz被替换为相应的大写字母ABCDEFGHIJKLMNOPQRSTUVWXYZ

参数

CH

-

字符被转换。如果ch的值不能表示为unsigned char并且不等于EOF,则行为是未定义的。

返回值

如果在当前C语言环境中未列出大写版本,则ch或unmodified ch的大写版本。

#include <stdio.h>
#include <ctype.h>
#include <locale.h>
#include <limits.h>
 
int main(void)
{
    /* In the default locale: */
    unsigned char u;
    for (unsigned char l=0; l<UCHAR_MAX; l++) {
        u = toupper(l);
        if (l!=u) printf("%c%c ", l,u);
    }
    printf("\n\n");
 
    unsigned char c = '\xb8'; // the character Ž in ISO-8859-15
                              // but ´ (acute accent) in ISO-8859-1 
    unsigned char c2 = c;   // for printing
    setlocale(LC_ALL, "en_US.iso88591");
    printf("in iso8859-1, toupper('0x%x') gives 0x%x\n", c2, toupper(c));
    setlocale(LC_ALL, "en_US.iso885915");
    printf("in iso8859-15, toupper('0x%x') gives 0x%x\n", c2, toupper(c));
}

输出:

aA bB cC dD eE fF gG hH iI jJ kK lL mM nN oO pP qQ rR sS tT uU vV wW xX yY zZ 
 
in iso8859-1, toupper('0xb8') gives 0xb8
in iso8859-15, toupper('0xb8') gives 0xb4

参考

  • C11标准(ISO / IEC 9899:2011):
    • 7.4.2.2 toupper功能(p:204)
  • C99标准(ISO / IEC 9899:1999):
    • 7.4.2.2 toupper功能(p:185)
  • C89 / C90标准(ISO / IEC 9899:1990):
    • 4.3.2.2 toupper功能

扩展内容

tolower

将字符转换为小写(函数)

towupper(C95)

将宽字符转换为大写(函数)

| 关于toupper的C ++文档 |

C

C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。