c - Re Legacy 代码 : format ‘%d’ expects argument of type ‘int’ , 但参数 3 的类型为 ‘long unsigned int’ [-Wformat]

标签 c unix gcc printf undefined-behavior

我经常尝试使用最近的 GCC 构建许多旧的模拟器以及磁盘和磁带归档工具。有些错误很容易修复,但我不是一个很好的程序员。

我明白了:

itstar.c: In function ‘addfiles’:
itstar.c:194:4: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat]
itstar.c:194:4: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘long unsigned int’ [-Wformat]

来自这个代码片段:

/* add files to a DUMP tape */
/* output buffer must have been initialized with resetbuf() */
static void addfiles(int argc,char **argv)
{
    int c=argc;
    char **v=argv;

    while(c--) {
        addfile(argc,argv,*v++);
    }
    if(verify)
        printf("Approximately %d.%d' of tape used\n",count/bpi/12,
            (count*10/bpi/12)%10);
}

第 194 行是倒数第三行,从 printf 开始。

文件是itstar.c,来自tapetools,代码here .

尽管有警告,它仍会生成,但我更想知道如何防止它,
因此结果更有效并且数据损坏的可能性更小。

拜托,我错过了什么,需要改变什么?

提前谢谢你。

最佳答案

使用格式说明符 %lu 而不是 %d 并且您的编译器应该停止提示。

printf("Approximately %lu.%lu' of tape used\n", count/bpi/12, (count*10/bpi/12)%10);

关于c - Re Legacy 代码 : format ‘%d’ expects argument of type ‘int’ , 但参数 3 的类型为 ‘long unsigned int’ [-Wformat],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20590823/

相关文章:

c - 为什么要包含头文件? #include 是如何工作的?

c - 优化 switch case 代码

java - 将字符串从 Java 传递到 bash 脚本

bash - 在bash脚本中并行下载文件

c - 模仿/模仿 C 中的大端行为?

c++ - gcc -O2 小于 gcc -O2 -g 后跟 strip --strip-all

C 控制台 - 在同一行打印图案

c - 在函数中返回 char*

c - 如何在C中定义 "constant"结构

c - 具有可变参数的函数 - 奇怪的输出