to 函数to Functions

每个 to 函数及其关联的宏(如果有),将单个字符转换为另一个字符。Each of the to functions and its associated macro, if any, converts a single character to another character.

__toascii__toascii toupper、_toupper、towuppertoupper, _toupper, towupper
tolower、tolower、towlowertolower, _tolower, towlower


to 函数和宏转换如下所示。The to functions and macro conversions are as follows.

例程所返回的值Routine Macro 描述Description
__toascii __toascii c 转换为 ASCII 字符Converts c to ASCII character
tolower tolower 如适用,将 c 转换为小写Converts c to lowercase if appropriate
_tolower _tolower c 转换为小写Converts c to lowercase
towlower None c 转换为相应的宽字符小写字母Converts c to corresponding wide-character lowercase letter
toupper toupper 如适用,将 c 转换为大写Converts c to uppercase if appropriate
_toupper _toupper c 转换为大写Converts c to uppercase
towupper None 将 c 转换为相应的宽字符大写字母Converts c to corresponding wide-character uppercase letter

若要将 的函数版本用于同时定义为宏的 例程,或者使用 #undef 指令移除宏定义或不包括 CTYPE.H。To use the function versions of the to routines that are also defined as macros, either remove the macro definitions with #undef directives or do not include CTYPE.H. 如果使用 /Za 编译器选项,编译器将使用 touppertolower 的函数版本。If you use the /Za compiler option, the compiler uses the function version of toupper or tolower. touppertolower 函数的声明位于 STDLIB.H 中。Declarations of the toupper and tolower functions are in STDLIB.H.

__toascii 例程集将除低顺序 7 位以外的所有 c 设置为 0,使转换后的值表示 ASCII 字符集中的字符。The __toascii routine sets all but the low-order 7 bits of c to 0, so that the converted value represents a character in the ASCII character set. 如果 c 已表示 ASCII 字符,则 c 保持不变。If c already represents an ASCII character, c is unchanged.

tolowertoupper 例程:The tolower and toupper routines:

  • 依赖于当前区域设置的 LC_CTYPE 类别(tolower 调用 isuppertoupper 调用 islower)。Are dependent on the LC_CTYPE category of the current locale (tolower calls isupper and toupper calls islower).

  • 如果 c 表示当前区域设置中大小写正确的可转换字母并且该区域设置存在相反的大小写,则转换 cConvert c if c represents a convertible letter of the appropriate case in the current locale and the opposite case exists for that locale. 否则,c 保持不变。Otherwise, c is unchanged.

    _tolower_toupper 例程:The _tolower and _toupper routines:

  • tolowertoupper 区域设置独立的、速度更快的版本。Are locale-independent, much faster versions of tolower and toupper.

  • 仅当 isascii(c)isupper(c)islower(c) 分别不为零时可用。Can be used only when isascii(c) and either isupper(c) or islower(c), respectively, are nonzero.

  • 如果 c 不是用于转换的正确大小写的 ASCII 字母,则结果不确定。Have undefined results if c is not an ASCII letter of the appropriate case for converting.

    当且仅当下列两个条件均不为零时,towlowertowupper 函数才返回转换后的 c 副本。The towlower and towupper functions return a converted copy of c if and only if both of the following conditions are nonzero. 否则,c 保持不变。Otherwise, c is unchanged.

  • c 是大小写正确的宽字符(即 iswupperiswlower 分别为非零值)。c is a wide character of the appropriate case (that is, for which iswupper or iswlower, respectively, is nonzero).

  • 目标大小写有对应的宽字符(即 iswloweriswupper 分别为非零值)。There is a corresponding wide character of the target case (that is, for which iswlower or iswupper, respectively, is nonzero).


// crt_toupper.c  
/* This program uses toupper and tolower to  
 * analyze all characters between 0x0 and 0x7F. It also  
 * applies _toupper and _tolower to any code in this  
 * range for which these functions make sense.  

#include <ctype.h>  
#include <string.h>  

char msg[] = "Some of THESE letters are Capitals.";  
char *p;  

int main( void )  
   printf( "%s\n", msg );  

   /* Reverse case of message. */  
   for( p = msg; p < msg + strlen( msg ); p++ )  
      if( islower( *p ) )  
         putchar( _toupper( *p ) );  
      else if( isupper( *p ) )  
         putchar( _tolower( *p ) );  
         putchar( *p );  
Some of THESE letters are Capitals.  

请参阅See Also

数据转换 Data Conversion
区域设置 Locale
is、isw 例程is, isw Routines