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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
Here is an example implementation of a function to compute the integral of any unary function using C language and pointers: ```c #include <stdio.h> double integral(double (*f)(double), double a, double b, int n) { double h = (b - a) / n; // width of each subinterval double sum = 0.0; // initialize the sum int i; for (i = 0; i < n; i++) { double x1 = a + i * h; double x2 = a + (i + 1) * h; double y1 = (*f)(x1); double y2 = (*f)(x2); double area = (y1 + y2) * h / 2.0; sum += area; } return sum; } // example usage double square(double x) { return x * x; } int main() { double a = 0.0; double b = 1.0; int n = 1000; double result = integral(&square, a, b, n); printf("Integral of x^2 from %g to %g with %d subintervals: %g\n", a, b, n, result); return 0; } ``` In this example, the `integral` function takes four arguments: a pointer to the unary function to integrate (`f`), the lower and upper bounds of integration (`a` and `b`, respectively), and the number of subintervals to use in the approximation (`n`). The function then computes the width of each subinterval (`h`), initializes the sum to 0, and iterates over the subintervals, computing the area under the curve for each and adding it to the sum. The final result is the sum of all the subinterval areas, which is returned by the function. To demonstrate the usage of this function, the `square` function is defined as an example of a unary function to integrate, and is passed as a pointer to the `integral` function along with the bounds of integration and number of subintervals. The result is printed to the console.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值