Pointers on C——9 Strings, Characters, and Bytes.9

来源:互联网 发布:ubuntu 播放器推荐 编辑:程序博客网 时间:2024/05/18 03:25

9.7 Error Messages

When calls are made to the operating system to perform functions, such as opening files, errors that occur are reported by setting an external integer variable called errno to an error code. The strerror function takes one of these error codes as an argument and returns a pointer to a message describing the error. The prototype of this function is:

当你调用一些函数,请求操作系统执行一些功能如打开文件时,如果出现错误,操作系统是通过设置-个外部的整型变量errno 进行错误代码报告的。strerror 函数把其中一个错误代码作为参数并返回一个指向用于描述错误的字符串的指针。这个函数的原型如下:

char *strerror( in error_number );


In fact, the returned value ought to be declared const, because you are not supposed to modify it.

事实上,返回值应该被声明为const ,因为你不应该修改它。


9.8 Character Operations

The library includes two groups of functions that operate on individual characters,prototyped in the include file ctype.h. The first group is used in classifying characters, and the second group transforms them.

标准库包含了两组函数,用于操作单独的字符,它们的原型位于头文件ctype.h 。第1 组函数用于对字符分类,而第2 组函数用于转换字符。


9.8.1 Character Classification

Each classification function takes an integer argument that contains a character value.The function tests the character and returns an integer true or false value. Table 9.1 lists the classification functions and the test that each performs.

每个分类函数接受一个包含字符值的整型参数。函数测试这个字符并返回一个整型值,表示真或假1 。表9.1 列出了这些分类函数以及它们每个所执行的测试。



9.8.2 Character Transformation

The transformation functions translate uppercase characters to lowercase and vice versa.

转换函数把大写字母转换为小写字母或者把小写字母转换为大写字母。


int tolower( int ch );

int toupper( int ch );


toupper returns the uppercase equivalent of its argument, and tolower returns the lowercase equivalent of its argument. If the argument to either function is not a character of the appropriate case, then it is returned unchanged.

toupper 函数返回其参数的对应大写形式, tolower 函数返回其参数的对应小写形式。如果函数的参数并不是一个处于适当大小写状态的字符(即toupper 的参数不是小写字母或tolower 的参数不是个大写字母),函数将不修改参数直接返回。


Testing or manipulating characters directly reduces a programʹs portability. For example, consider the following statement, which attempts to test whether ch contains an uppercase character.

直接测试或操纵字符将会降低程序的可移植性。例如,考虑下面这条语句,它试图测试ch 是否是一个大写字符。


if( ch >= 'A' && ch <= 'Z' )


This statement works on machines using the ASCII character set, but fails on machines using the EBCDIC character set. On the other hand, the statement if( isupper( ch ) ) will work correctly with any character set.

这条语句在使用ASCII 字符集的机器上能够运行,但在使用EBCDIC 字符集的机器上将会失败。另一方面,下面这条语句if( isupper( ch ) ) 无论机器使用哪个字符集,它都能顺利运行。

上一章 Pointers on C——9  Strings, Characters, and Bytes.8

阅读全文
0 0