gpt4 book ai didi

c - 警告 : format ‘%s’ expects argument of type ‘char *’ , 但参数 2 的类型为 ‘__dev_t’ [-Wformat=]

转载 作者:太空宇宙 更新时间:2023-11-04 12:07:55 25 4
gpt4 key购买 nike

我在程序中使用 stat 文件系统,我想使用

打印设备 ID

printf("\nst_dev = %s\n",buf.st_dev);

但是我得到了错误:

warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘__dev_t’ [-Wformat=]

这里 b 应该用什么代替 %s?

最佳答案

st_dev 属于 dev_t 类型,根据 POSIX definition整数类型 :

dev_t shall be an integer type.

所以使用 %s 打印它肯定是错误的。没有可移植的方式来打印它,因为在 POSIX 中没有为它定义格式说明符。您可以使用 intmax_t 来打印它:

printf("\nst_dev = %jd\n", (intmax_t)buf.st_dev);

如果 intmax_t 不可用(例如 C89 系统),那么您可以将其转换为 long

关于c - 警告 : format ‘%s’ expects argument of type ‘char *’ , 但参数 2 的类型为 ‘__dev_t’ [-Wformat=],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49963614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com