简体   繁体   中英

Why stdio.h, stdlib.h, string.h declare size_t, NULL, etc. instead of #include <stddef.h>?

The headers stdio.h , stdlib.h , string.h declare size_t , NULL , etc.

Why declaring size_t , NULL , etc. instead of #include <stddef.h> ?

Why declaring size_t, NULL, etc. instead of #include <stddef.h> ?

Per C17 7.19/3, stddef.h does define NULL , so the question is unfounded. Inasmuch as the premise seems to be in part that the role served by stddef.h makes it a good place for NULL to be defined, the standard committee seems to agree.

stdio.h (7.21.1/2), stdlib.h (7.22/3), and string.h (7.24.1/1) also define NULL . As to why this is, I am inclined to say that a combination of historical variation in which headers defined which macros plays a part, but it is also the case that each of these headers declare at least one function whose arguments and / or return value attribute specific significance to null pointers. For example, fopen() returns a null pointer when it fails. Although it is not essential to use the macro NULL to represent a null pointer, it is highly idiomatic, so ensuring that these headers define it makes it accessible in at least most of the standard library cases where it will be wanted, without requiring inclusion of a header just for that purpose.

Because that's what the C standard says to do:

http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

ISO/IEC 9899:TC3 Committee Draft — Septermber 7, 2007 WG14/N1256

7.19 Input/output <stdio.h>

7.19.1 Introduction

1 The header <stdio.h> declares three types, several macros, and many functions for performing input and output.

2 The types declared are size_t (described in 7.17);

[...]

3 The macros are NULL (described in 7.17);

7.17 is in fact the section for <stddef.h> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM