c标准库中还缺少很多函数,例如投有图形函数,甚至没有全屏幕文本操作函数,signal机制也相当弱(见12.10),并且根本没有对多任务或使用常规内存以外的内存提供支持。尽管C标准库存在上述缺陷,但它毕竟为所有的程序都提供了一套基本功能,不管这些程序是运行在多任务、多窗口的环境下,还是运行在简单的终端上,或者是运行在一台昂贵的烤面包机上。
C标准库中所缺的函数可以从其它途径获得,例如编译程序开发商和第三方的函数库都会提供一些函数,这些函数都是事实上的标准函数。然而,标准库中的函数已经为程序设计提供了一个非常坚实的基础。
12.1 为什么应该使用标准库函数而不要自己编写函数?
标准库函数有三点好处:准确性、高效性和可移植性。
准确性:编译程序的开发商通常会保证标准库函数的准确性。更重要的是。至少开发商做了全面的检测来证实其准确性,这比你所能做到的更加全面(有些昂贵的测试工具能使这项工作更加容易)。
高效性:优秀的C程序员会大量使用标准库函数,而内行的编译程序开发商也知道这一点。如果开发商能提供一套出色的标准库函数,他就会在竞争中占优势。当对相互竞争的编译程序的效率进行比较时,一套出色的标准库函数将起到决定性的作用。因此,开发商比你更有动力,并且有更多的时间,去开发一套高效的标准库函数。
可移植性:在软件要求不断变化的情况下,标准库函数在任何计算机上,对任何编译程序都具有同样的功能,并且表达同样的含义,因此它们是C程序员屈指可数的几种依靠之一。
有趣的是,你很难找到一项关于标准库函数的最标准的信息。对于每一个函数,都需要有一个(在极少数情况下需要两个)保证能将该函数的原型提供给你的头文件(在调用任何一个函数时,都应该包含其原型,见8.2)。有趣的是什么呢?这个头文件可能并不是真正包含该函数原型的文件,在有些(非常糟糕!)情况下,甚至由编译程序手册推荐的头文件都不一定正确。对于宏定义,typedef和全局变量,同样会发生这种情况。
为了找到“正确的”头文件,你可以在一份ANSI/ISO c标准的拷贝中查阅相应的函数。如果你手头没有这样一份拷贝,你可以使用表12.2。
请参见:
8.2为什么要使用函数原型?
12.2 为了定义我要使用的标准库函数,我需要使用哪些头文件?
12.2 为了定义我要使用的标准库函数,我需要使用哪些头文件?
你需要使用ANSI/ISO标准规定的你应该使用的那些头文件,见表12.2。
有趣的是,这些文件并不一定定义你要使用的函数。例如,如果你要使用宏EDOM,你的编译程序保证你能通过包含(errno.h)得到这个宏,而(errno.h)可能定义了宏EDOM,也可能只包含定义这个宏的头文件。更糟的是,编译程序的下一个版本可能会在另一个地方定义宏EDOM。
因此,你不用去寻找真正定义一个函数的头文件并使用这个文件,而应该使用那个被假定为定义了该函数的头文件,这样做是肯定可行的。
有几个名字在多个头文件中被定义:NULL,size_t和wchar_t。如果你需要其中一个名字的定义,可以使用任意一个定义了该名字的头文件((stddef.h>是一个较好的选择,它不仅小,而且包含了常用的宏定义和类型定义)。
表12.2标准库函数的头文件
----------------------------------------------------------------------
函数 头文件
----------------------------------------------------------------------
abort stdlib. h
abs stdlib. h
acos math. h
asctime time. h
asin math. h
assert assert.h
atan math. h
atan2 math. h
atexit stdlib. h
atof stdlib. h
atoi stdlib. h
atol stdlib. h
bsearch stdlib. h
BUFSIZ stdio. h
calloc stdlib. h
ceil math. h
clearerr stdio. h
clock time. h
CLOCKS-PER-SEC time. h
clock_t time. h
cos math. h
cosh math. h
ctime time. h
difftime time. h
div stdlib. h
div_t stdlib. h
EDOM errno. h
EOF stdio. h
ERANGE errno. h
errno errno. h
exit stdlib. h
EXIT_FAILURE stdlib. h
EXIT_SUCCESS stdlib. h
exp math. h
fabs math. h
fclose stdio. h
feof stdio.h
ferror stdio.h
fflush stdio. h
fgetc stdio.h
fgetpos stdio. h
fgets stdio.h
FILE stdio. h
FILENAME-MAX stdio. h
floor math. h
fmod math. h
fopen stdio. h
FOPEN_MAX stdio. h
fpos_t stdio. h
fpnntf stdio. h
fputc stdio.h
fputs stdio. h
head stdio. h
free stdlib. h
freopen stdio. h
frexp math. h
fscanf stdio. h
fseek stdio. h
fsetpos stdio. h
ftell stdio. h
fwrite stdio. h
getc stdio.h
getchar stdio. h
getenv stdlib. h
gets stdio.h
gmtime time. h
HUGE-VAL math.h
_IOFBF stdio. h
_IOLBF stdio. h
_IONBF stdio. h
isalnum ctype. h
isalpha ctype. h
iscntrl ctype. h
isdigit ctype. h
isgraph ctype. h
islower ctype. h
isprint ctype. h
ispunct ctype. h
isspace ctype. h
isupper ctype. h
isxdigit ctype. h
jmp_buf setjmp. h
labs stdlib. h
LC_ALL locale. h
LC_COLLATE locale. h
LC_CTYPE locale. h
LC_MONETARY locale. h
LC_NUMERIC locale. h
LC_TIME locale. h
struct lconv locale. h
ldexp math. h
ldiv stdlib. h
ldiv_t stdlib. h
localeconv locale. h
localtime time. h
log math. h
log10 math. h
longjmp setjmp. h
L_tmpnam stdio. h
malloc stdlib. h
mblen stdlib. h
mbstowcs stdlib. h
mbtowc stdlib. h
MB_CUR_MAX stdlib. h
memchr