glibc 的文档保留它们是整数类型(不比 unsigned int 窄),但我没有找到说明它们必须是整数类型的标准参考(另请参阅 time_t )。
所以最后,问题就变成了:
#include <stdio.h>
#include <stdint.h>
struct stat st;
if (stat("somefile", &st) == 0) {
printf("%ju %ju\n", (uintmax_t)st.st_dev, (uintmax_t)st.st_ino);
}
便携的。
POSIX http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html标准要求dev_t
是一个整数类型并且ino_t
是一个无符号整数。
dev_t 应为整数类型。
fsblkcnt_t、fsfilcnt_t 和 ino_t 应定义为无符号整数
类型。
Since intmax_t
and uintmax_t
应该是“最大宽度”整数,你的代码是安全的。
只是为了确定以防万一st_dev
恰好是负数,你可以写成:
printf("%jd %ju\n", (intmax_t)st.st_dev, (uintmax_t)st.st_ino);
否则,您的代码是安全的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)