c - 为什么我看到的每个编程教程中都无缘无故地包含 sys/types ?

标签 c systypes.h

最近我看了很多 C 语言的套接字编程教程。在每个视频中,都包含头文件 sys/types,但是当我运行编写的代码时在没有 sys/types 的视频中,我没有收到任何警告或错误。这个头文件有什么作用以及为什么如此常见?

最佳答案

该文件定义了其他文件中使用的许多类型。在较旧的系统上,有必要将其包含在其他系统 header 之前。

来自 socket 系统调用的手册页:

SYNOPSIS
       #include <sys/types.h>          /* See NOTES */
       #include <sys/socket.h>

       int socket(int domain, int type, int protocol);

...

NOTES
       POSIX.1 does not require the inclusion of <sys/types.h>, and this
       header file is not required on Linux.  However, some historical (BSD)
       implementations required this header file, and portable applications
       are probably wise to include it.

关于c - 为什么我看到的每个编程教程中都无缘无故地包含 sys/types ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61446189/

相关文章:

c - 从函数返回一个二维数组到主函数

c - 没有这样的文件或目录

c - C编译器是否区分用户代码和库代码中定义的类型?

c - 为什么 gid_t 不在我的 sys/types 头文件中?

将非 GUI Makefile (make) 项目转换为 KDevelop

c - 这段代码有什么不同

c - scanf() 格式字符串中尾随空格有何影响?

c - 结构指针初始化错误