如果我使用 sizeof 和 size_t 我是否应该始终包含 stddef.h

发布于 2024-08-27 20:52:33 字数 189 浏览 14 评论 0原文

如果我在代码中使用 sizeof 运算符并使用 size_t,我是否必须包含 stddef.h?我没有包含 stddef.h,并且我的代码在使用 MVS2008 和 Borland C++ BuilderX 进行编译时不会发出警告。

多谢...

if I'm using the sizeof operator and making use of size_t in 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.

Thanks a lot...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

饮湿 2024-09-03 20:52:34

sizeof() 虽然看起来像函数调用,但实际上是一个运算符,是语言核心的一部分。无需包含。

size_t 在各种标头中定义:stddef.hstring.hstdlib.h>stdio.h。包含其中任何一个就足以在代码中使用 size_t

sizeof(), while looking like a function call, is actually an operator and part of the language core. No include needed.

size_t is defined in various headers: stddef.h, string.h, stdlib.h, and stdio.h. Including any one of them is enough to use size_t in your code.

风为裳 2024-09-03 20:52:34

不,您可以包含一个标头,该标头又包含 stddef.h

size_t 定义应通过包含 stdlib.h 头文件提供给引用代码段。事实上,大多数实现没有在此文件中按字面定义它,而是sub包含文件stddef.h作为标准GNU C 编译器库可以做到这一点。直接包含应用程序代码的 stddef.h 是完全有效的,因此在不需要或不需要此文件中的其他成员的情况下可以替换 stdlib.h。

来源

No, you can include a header which in turn includes stddef.h

The size_t definition shall be provided to a referencing piece of code by including stdlib.h header file. In fact most implementations don't have it defined literally in this file but instead do sub include the file stddef.h as 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.

Source

ˇ宁静的妩媚 2024-09-03 20:52:34

在 c 中,size_t 的定义来自以下几个标头之一:stddef.hstdio.hstdlib.h、string.htime.hwchar.h

编译器实现可以通过多种方式来实现此目的,但请注意,不能使用的一种方式是让编译器在您背后为您包含这些标头之一 - 即不允许 C 编译器执行此操作(C++ 取消了此限制,允许出于自身目的包含任何标准头文件)。

In c the definition for size_t comes from one of several headers: stddef.h, stdio.h, stdlib.h, string.h, time.h or wchar.h.

There are any number of ways that the compiler implementation can arrange for this, but note that one way that can't be 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文