上章回顾
数组和指针相同与不同
通过指针访问数组和通过数组访问指针
指针在什么时候可以加减运算
函数指针的申明和调用
函数数组和数组函数
git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.gitgit@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.git
附录二
附录二
C语言标准库
C语言标准库
git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.gitgit@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.git
预习检查
NULL和NUL有什么差别 字符串操作有哪几个常用函数
memcpy和strcpy有什么差别
git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.gitgit@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.git
课程目标
本章概述
重点
难点
对标准C语言的补充,以及一些常用的特性。
本章目标
了解一些C语言特殊的宏定义 掌握函数可变参数的应用
理解NULL, offsetof, errno宏或函数
掌握函数可变参数列表难点 C语言库的日期函数操作
函数的可变参数列表 标准C的异常处理机制
git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.gitgit@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.git
本章结构
C标准语言库概要和应用 C标准语言库概要和应用
git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.gitgit@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.git
标准语言库概述
标准语言库概述
标准C语言函数 标准C语言函数
主要库文件 主要库文件
标准库应用总结
标准库应用总结
2.1 标准语言概述 函数库简介
C语言标准库的历史 标准库函数优势 独立环境和宿主环境
git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.gitgit@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.git
2.1.1 函数库简介 标准c语言包括语言标准和一组标准库
支持字符和字符串、输入与输出、数学函数、期与时
间转换、动态存储分配和其他特性
预处理器命令#include,引用这个库的头文件
例:
下列程序段中头文件math.h使程序能够访问余弦函数cos。 #include <math.h>
double x,y;
x = cos(y);
git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.gitgit@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.git
2.1.1 函数库简介 对定义为函数的库功能,标准C语言允许实现提供除真
正函数以外的同名函数式宏
例:
假设担心math.h中已有名为cos的宏,则可以用下面两种
方法引用基础函数。两者都利用宏名后面不能紧跟一个开 括号的特点,避免扩展同名函数或宏cos。
#include<math.h>
double a,b,(*p)(double);
...
p = &cos;
a= (*p)(b); /* calIs function cos,always */ a= (cos)(b); /* calls function cos,always */
也可以取消所有涉及到的宏的定义: #include<math.h>
#undef cos
...
嵌入式家园 www.embedclub.com
a = cos(b); /* calls function cos,always */
git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.git
2.1.2 C语言标准库的历史 C语言提供的标准函数库,函数库通过#include进行引用
在C89标准中:
<assert.h> <ctype.h> <errno.h> <float.h> <limits.h> <locale.h> <math.h> <setjmp.h> <signal.h> <stdarg.h> <stddef.h> <stdio.h> <stdlib.h> <string.h> <time.h>
在95年的修正版中
<iso646.h> <wchar.h> <wctype.h>
在C99中增加了六个函数库
<complex.h> <fenv.h> <inttypes.h> <stdbool.h> <stdint.h>
<tgmath.h>
git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.gitgit@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.git
2.1.3 标准库函数优势
准确性 高效性 可移植性
git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.gitgit@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.git
2.1.4 独立环境和宿主环境
“独立(free—standing)”系统 嵌入式系统可以不要任何类型的文件系统,也可以基本上不
要操作系统
不要求它们提供除语言本身以外的任何东西
宿主(hosted)”环境 程序运行在RC机、大型机或者介于两者之间的计算机上
git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.gitgit@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.git
2.2 标准C语言函数 标准C语言函数
C++兼容性
git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.gitgit@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.git
2.2.1标准C语言函数 如何从标准c语言定义得到传统c语言库函数定义
消除任何使用标准c语言类型的函数,如long long与_Complex,或 消除标准c语言中新增的函数(C89或C99).
删除限定符const、restrict与volatile 删除数组声明符括号内使用的static. 将类型void * 换成char * ,将size_t换成int。
git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.gitgit@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.git
2.2.1标准C语言函数 库函数注意事项
库名原则上是保留字。编程人员不能定义与标准库名称同名的外部
对象。
库头文件或文件名可以内置在实现中,但仍然要被包括之后才能访
问其名称
编程人员可以多次按任意顺序包括库头文件
例 下面的方法可以保证库头文件不被包括多次: /* I Header Btddef.H */
#ifndef _STDDEF /* Don.t try to redeclare */ #define _STDDEF l
typedef int ptrdiff_t; .../* 0ther definitions */
git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.git#endif
git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.git
2.2.2 C++兼容性 C++语言包括标准C语言运行库,但增加了几个C++
特定库 不能从c语言程序中调用c++函数,但c++提供了从
C++中调用c语言函数的方法 在C++中,声明c语言函数时有两个要求
函数声明要使用标准c语言原型,因为c++要求原型。 外部c语言要显式地标为具有c语言连接,即在c++的存储类extern后面加
上字符串“C”。
git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.gitgit@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.git
2.2.2 C++兼容性 c语言函数中调用另一C语言函数
extern int f(void); c++中调用C语言函数
extern“C“int f(void); c++中要声明一组c语言函数,则可以对所有c语言函
数采用连接规范:
extern“C“{
double sqrt(double x);
int f(void);
......
}
git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.gitgit@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.git
2.2.2 C++兼容性 从C语言或c++调用头文件library.h
在头文件中包括extern “C”声明(条件预编译__cplusplus宏),表 示这是个c++程序。
/* File library.h */ #ifdef __cplusplus
extern“c” { #endif
/* C declarations */ #ifdef __cplusplus
}
用正常c语言声#e明ndif编写头文件,要求c++用户用# include命令包装连接声明
extern “C“ { #include”library.h“
git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.git}
git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.git
2.3 主要的库文件
stddef.h math.h ctype.h erron.h stdbool.h iso645.h assert.h
stdio.h stdlib.h string.h stdarg.h time.h setjmp.h signal.h
git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.gitgit@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.git
2.3.1 Stddef.h
语法概要之定义
#include<stddef.h> #define NULL ......
typedef ...
ptrdiff_t; size_tj wchar_t;
typedef ...
typedef ...
#define offsetof(type,member-designator)
git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.gitgit@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.git
2.3.1 Stddef.h
NULL
size_t
null指针常量
0 转换为类型void的0
ptrdiff_t
实现定义的带符号整型
是两个指针相减所得到的类型
sizeof运算符得到的无符号整型
offsetof
扩展一个整型常量表达式
#define offsetof(type,memb)((size_t)&((type*)0)->memb)
git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.gitgit@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.git
2.3.1 Stddef.h
例:对于sizeof(int)为4的字节寻址计算机
#include<Stddef.h>
struct s{int a; int b;}x;
程序运行结果是:
diff的值为1 size的值为4
offset的值为4
size_t size,offset; ptrdiff_t diff;
Diff = &x.b-&x.a;
Size = sizeof(x.a); offset=offsetof(struct s,b);
git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.gitgit@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.git
2.3.2 math.h
双精度浮点数的精度进行操作
参数传递和错误处理
如果传递过来的参数不在其定义域内,函数会返回一些不确定的值 ,并将变量errno置为EDOM。
如果返回值太大,无法用一个double类型表示(造成上溢),这些函数 会返回HUGEVAL,并将errno置为ERANGE。
如果函数结果太小,无法表示,则函数返回0,errno是否设置为 ERANGE由实现确定
EDOM,ERANGE和HUGEVAL都在math.h中定义。
git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.gitgit@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.git
2.3.3 ctype.h
字符处理有两类函数:分类与转换
分类函数的名称以is开头 转换函数的名称以to开头
宽字符进行运算的分类与转换函数
分类函数----isw 转换函数----tow
git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.gitgit@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.git
2.3.3 ctype.h
字符处理有两类函数:分类与转换
分类函数的名称以is开头 转换函数的名称以to开头
宽字符进行运算的分类与转换函数
分类函数----isw 转换函数----tow
通用函数
分类函数wcstrans与iswctrans 转换函数 wctrans与towctrans
git@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.gitgit@github.com:Kevin-Dfg/Data-Structures-and-Algorithm-Analysis-in-C.git
2.3.3 ctype.h
例:
#inelude<ctype.h> #define TRUE 1 #define FALSE 0
int is_id(const char *s) {
}
char ch;
if ((ch= *s++) ==‘