C语言 如果我使用 sizeof 和 size_t,我是否应该总是包含 stddef.h
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2605217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Should I always include stddef.h if I use sizeof and size_t
提问by yCalleecharan
if I'm using the sizeofoperator and making use of size_tin my code, do I have necessarily have to include stddef.h? I haven't included stddef.h, and my code compiles without warning with both MVS2008 and with Borland C++ BuilderX.
如果我使用sizeof运算符并size_t在我的代码中使用,我是否必须包含stddef.h?我没有包含stddef.h,并且我的代码在使用 MVS2008 和 Borland C++ BuilderX 编译时没有警告。
Thanks a lot...
非常感谢...
回答by DevSolar
sizeof(), while looking like a function call, is actually an operator and part of the language core. No include needed.
sizeof(),虽然看起来像一个函数调用,但实际上是一个运算符和语言核心的一部分。不需要包含。
size_tis defined in various headers: stddef.h, string.h, stdlib.h, and stdio.h. Including any one of them is enough to use size_tin your code.
size_t在各种标头被定义:stddef.h,string.h,stdlib.h,和stdio.h。包括其中任何一个就足以size_t在您的代码中使用。
回答by codaddict
No, you can include a header which in turn includes stddef.h
不,您可以包含一个标头,而标头又包含 stddef.h
The size_t definition shall be provided to a referencing piece of code by including stdlib.hheader file. In fact most implementations don'thave it defined literally in this file but instead do subinclude the file stddef.has for example the standard library of the GNU C compiler does. The direct inclusion of stddef.h for application code is totally valid and thus can replace stdlib.h in cases where no other members from this file are needed or desired.
size_t 定义应通过包含stdlib.h头文件提供给引用代码段。事实上,大多数实现并没有在这个文件中逐字定义它,而是 像 GNU C 编译器的标准库那样将文件stddef.h子包含进来。应用程序代码直接包含 stddef.h 是完全有效的,因此可以在不需要或不需要此文件中的其他成员的情况下替换 stdlib.h。
回答by Michael Burr
In c the definition for size_tcomes from one of several headers: stddef.h, stdio.h, stdlib.h, string.h, time.hor wchar.h.
在 c 中, for 的定义size_t来自以下几个标头之一:stddef.h、stdio.h、stdlib.h、string.h、time.h或wchar.h。
There are any number of ways that the compiler implementation can arrange for this, but note that one way that can'tbe used is by having the compiler include one of these headers for you behind your back - that's not something a C compiler is permitted to do (this restriction was lifted for C++, which is allowed to include any of the standard headers for its own purposes).
编译器实现可以通过多种方式对此进行安排,但请注意,一种不能使用的方法是让编译器在背后为您包含这些头文件之一 - 这不是 C 编译器所允许的要做(此限制已针对 C++ 解除,允许出于自身目的包含任何标准头文件)。

