为什么fgets接受int而不是size_t? [英] Why does fgets accept an int instead of a size_t?
问题描述
出于明显的原因,诸如strcpy()
,malloc()
,strlen()
之类的函数以及其他各种函数都接受其参数或将返回值作为size_t
而不是int
或unsigned int
来接收.
Functions such as strcpy()
, malloc()
, strlen()
and various others accept their arguments or return values as a size_t
instead of a int
or an unsigned int
for obvious reasons.
某些文件功能(例如fread()
和fwrite()
)也使用size_t
.通过扩展,可以预期char* fgets (char *str, int num, FILE *stream)
应该使用size_t
而不是int
作为其缓冲区大小的参数.
Some file functions such as fread()
and fwrite()
use size_t
as well. By extension, it would be expected that char* fgets (char *str, int num, FILE *stream)
should use a size_t
and not an int
as argument for its buffer size.
但是, fgets()
使用int
.为什么有任何客观的解释?
However, fgets()
uses an int
. Is there any objective explanation why?
推荐答案
原始K& R在p.155上使用int
参数定义了fgets()
.书中介绍的代码本来可以工作
也使用unsigned int
(它使用>0
,但是编写了循环,所以永远不会低于零).
The original K&R defined fgets()
on p.155 with an int
argument. The code presented in the book would have worked
with an unsigned int
as well (it uses a >0
, but the loop is written so to never go below zero).
size_t
稍后在 C89 (ANSI C)中作为sizeof()
的类型进行介绍.由于此功能是专门为协调内存分配而引入的,因此内存
管理功能和字符串功能也进行了相应更新.但是文件I/O不是:在C89中唯一使用size_t
的文件功能是那些新功能.
由C89引入并在K& R中不存在,例如fread()
/fwrite()
.是的,K& R没有这些功能
并仅依靠使用文件描述符的(不可移植的)unix读/写功能进行块操作.
size_t
got introduced later, in C89 (ANSI C), as the type of sizeof()
. As this feature was specifically introduced for harmonizing memory allocation, memory
management functions and string functions were updated accordingly. But file I/O wasn't: the only file functions that used size_t
in C89 are those new ones
introduced by C89 and did not exist in K&R such as for example fread()
/fwrite()
. Yes, K&R didn't have these functions
and relied for bloc operations only on (non portable) unix read/write functions using file descriptors.
应注意的是, POSIX标准已经统一了unix函数,与
ANSI C标准和于1988年末发布.该标准协调了许多使用size_t
的unix函数,因此现在read()
/write()
用size_t
定义.但是对于C标准库功能(例如fgets()
),POSIX优先于C标准
(标准当前版本的措词):
It shall be noted that the POSIX standard, which has harmonized the unix functions, was developed in parallel to
the ANSI C standard and issued late 1988. This standard has harmonized many unix functions to use size_t
so that read()
/write()
nowadays
are defined with size_t
. But for the C standard library functions such as fgets()
, POSIX gives precedence to the C standard
(wording of the current version of the standard):
此参考页上描述的功能符合ISO C标准. 这里描述的要求与ISO C标准之间的任何冲突都是无意的.
The functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional.
因此,具有讽刺意味的是,在POSIX中,fgets()
仍然继承自其历史K& R int
.
So in POSIX also, ironically, fgets()
still inherited from its historical K&R int
.
其他阅读内容
additional reading
- 从K& R到Ansi C -多布博士(Dr.Dobb's)-1989年:
- Going from K&R to Ansi C - Dr.Dobb's - 1989:
stdio.h:此标头定义和原型化了K& R第7章中列出的大多数功能.几乎没有(如果有)更改 在K& R中可以找到一些定义,但已添加了几个新功能.
stdio.h: This header defines and prototypes most of the functions listed in Chapter 7 of K&R. Few, if any, changes were made in the definitions found in K&R but several new functions have been added.
这篇关于为什么fgets接受int而不是size_t?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!