在C语言程式设计里,C 标准函式库(C Standard library)收集所有目前符合标准的表头档(head file),以及常用的函式库实作程式,例如I/O 输入输出和字串控制。不像COBOL、Fortran和PL/I等程式语言,在 C 语言的工作任务里不会包含嵌入的关键字,所以几乎所有的 C 语言程式都是由标准函式库的函式来建立的。
设计每一个函式的名称与特性会被写成一个电脑档案,这个档案就称为标头档案,但是实际的函式实作是被分存到函式库档案里。标头档的命名和领域是很常见的,但是函式库的组织架构也会因为不同的编译器而有所不同。标准函式库通常会随附在编译器上。因为 C 编译器常会提供一些额外的非ANSI C函式功能,所以某个随附在特定编译器上的标准函式库,对其他不同的编译器来说,是不相容的。
设计品质大多 C 标准函式库在设计上做得相当不错。有些少部分的,会为了商业优势和利益,会把某些旧函式视同错误或提出警告。字串输入函式 gets()(以及 scanf() 读取字串输入的使用上)是很多缓冲区溢位的原因,而且大多的程式设计指南会建议避免使用它。另一个较为奇特的函式是 strtok(),它原本是作为早期的词汇分析用途,但是它非常容易出错(fragile),而且很难使用。
标准函数C语言中没有固定的输入输出语句,I/O功能以函数调用的形式实现。这样做不仅提高了编译程序的效率,而且也使C语言的可移植性大大提高。
C语言的标准I/0函数都是面向文件的,即输入输出操作是以对文件读写的形式进行的,它反映,C与UNIX的内在联系。
C语言的标准I/O函数大致可以分为四类,即字符I/O函数、字符串I/O函数、流式文件I/O函数和格式化输入输出函数。
字符函数其中,用于从标准输入stdin或流式文件读取字符的函数有:
getc()
getchar()
fgetc()
用于向标准输出stdout或流式文件写字符的函数有:
putc()
putchar()
fputc()
这一类函数中有些是以宏的形式实现的,使用时应注意它们的副作用。
字符串函数字符串I/O函数主要有:
gets()
puts()
fgets()
fputs()
以上函数用于字符串的输入输出。对于标准输入,字符串由换行符结束,读入后换行符由空白符7\O’替代;对标准输出,则将空白符换成换行符写出。
流式文件函数流式文件I/O函数用于对指定的流式文件进行打开、关闭和读写等操作。在UNIX中,文件是无格式的字节流,而不存在象记录那样的结构。这类函数包括:
fopen() 打开文件
fclose() 关闭文件
fread() 读文件
furite() 写文件
fseek() 文件读写指针定位
fflush() 清文件缓冲区
格式化输入输出函数最基本的格式化I/O函数是printf(标准输出)和scanf(标准输入)。此外还有派生的fprintf和fscanf、sprintf和sscanf等。格式化I/O函数用于格式化数据的输入输出,这些函数的参数个数是可变的,但必须包括一个控制字符串,控制字符串决定输入或输出数据的类型、精度和格式。1
历史沿革1995年,Normative Addendum1 (NA1)批准了三个表头档(iso646.h, wchar.h, and wctype.h)增加到C标准函式库中。C99标准增加了六个表头档(complex.h, fenv.h, inttypes.h, stdbool.h, stdint.h, and tgmath.h)。C11标准中又新增了5个表头档(stdalign.h, stdatomic.h, stdnoreturn.h, threads.h, and uchar.h)。至此,C标准函式库共29个表头档:
Contains the assert macro, used to assist with detecting logical errors and other types of bug in debugging versions of a program. | ||
C99 | A set of functions for manipulating复数. | |
Defines set of functions used to classify characters by their types or to convert between upper and lower case in a way that is independent of the used character set (typicallyASCIIor one of its extensions, although implementations utilizingEBCDICare also known). | ||
For testing error codes reported by library functions. | ||
C99 | Defines a set of functions for controlling浮点数environment. | |
Defines macro constants specifying the implementation-specific properties of the浮点数library. | ||
C99 | Defines exact width integer types. | |
NA1 | Defines several macros that are equivalent to some of the operators in C. For programming inISO 646variant character sets. | |
Defines macro constants specifying the implementation-specific properties of the integer types. | ||
Defines C localization functions. | ||
Defines C mathematical functions. | ||
Declares the macros setjmp and longjmp, which are used for non-local exits. | ||
Defines C signal handling functions. | ||
C11 | For querying and specifying the data structure alignment of objects. | |
For accessing a varying number of arguments passed to functions. | ||
C11 | For atomic operations on data shared between threads. | |
C99 | Defines a boolean data type. | |
Defines several useful types and macros. | ||
C99 | Defines exact width integer types. | |
Defines core input and output functions | ||
Defines numeric conversion functions, pseudo-random numbers generation functions, dynamicmemory allocation, process control functions | ||
C11 | For specifying non-returning functions. | |
Defines C string handling functions. | ||
C99 | Defines type-generic mathematical functions. | |
C11 | Defines functions for managing multiple threads as well as mutexes and condition variables. | |
Defines date and time handling functions | ||
C11 | Types and functions for manipulatingUnicodecharacters. | |
NA1 | Defines wide string handling functions. | |
NA1 | Defines set of functions used to classify wide characters by their types or to convert between upper and lower case | |
本词条内容贡献者为:
胡启洲 - 副教授 - 南京理工大学C 标准函式库
图文简介
在C语言程式设计里,C 标准函式库(C Standard library)收集所有目前符合标准的表头档(head file),以及常用的函式库实作程式,例如I/O 输入输出和字串控制。不像COBOL、Fortran和PL/I等程式语言,在 C 语言的工作任务里不会包含嵌入的关键字,所以几乎所有的 C 语言程式都是由标准函式库的函式来建立的。
- 来源: 科普中国科学百科
- 上传时间:2018-06-14
科普中国公众号
科普中国微博

帮助