C语言 “%d”需要“int”类型的参数,但参数 2 的类型为“long unsigned int”[-Wformat=]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21128092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:37:37  来源:igfitidea点击:

'%d' expects argument of type 'int', but argument 2 has type 'long unsigned int' [-Wformat=]

ccompilationprintfsizeof

提问by LuizGsa13

I keep getting compile warnings but I don't know how to fix it:

我不断收到编译警告,但我不知道如何修复它:

'%d' expects argument of type 'int', but argument 2 has type 'long unsigned int' [

The program runs fine but I still get the compile warnings:

程序运行良好,但我仍然收到编译警告:

/* Sizeof.c--Program to tell byte size of the C variable */
#include <stdio.h>

int main(void) {
    printf("\nA Char is %d bytes", sizeof( char ));
    printf("\nAn int is %d bytes", sizeof( int ));
    printf("\nA short is %d bytes", sizeof( short ));
    printf("\nA long is %d bytes", sizeof( long ));
    printf("\nA long long is %d bytes\n", sizeof( long long ));
    printf("\nAn unsigned Char is %d bytes", sizeof( unsigned char ));
    printf("\nAn unsigned int is %d bytes", sizeof( unsigned int));
    printf("\nAn unsigned short is %d bytes", sizeof( unsigned short ));
    printf("\nAn unsigned long is %d bytes", sizeof( unsigned long ));
    printf("\nAn unsigned long long is %d bytes\n",
            sizeof( unsigned long long ));
    printf("\nfloat is %d bytes", sizeof( float ));
    printf("\nA double is %d bytes\n", sizeof( double ));
    printf("\nA long double is %d bytes\n", sizeof( long double ));

    return 0;

}

回答by Shafik Yaghmour

sizeofreturns size_tyou need to use %zufor the format string instead of %d. The type of unsigned integerof size_tcan vary (depending on platform) and may not be long unsigned inteverywhere, which is covered in the draft C99 standard section 6.5.3.4The sizeof operatorparagraph 4:

sizeof返回size_t您需要%zu用于格式字符串而不是%d. 所述类型的无符号整数size_t可以变化(取决于平台),也有可能长UNSIGNED INT无处不在,其在C99草案标准部覆盖6.5.3.4sizeof运算符4

The value of the result is implementation-defined, and its type (an unsigned integer type) is size_t, defined in (and other headers).

结果的值是实现定义的,它的类型(无符号整数类型)是 size_t,定义在(和其他头文件)中。

Also note that using the wrong format specifier for printfis undefined behavior, which is covered in section 7.19.6.1The fprintf function, which also covers printfwith respect to format specifiers says:

另请注意,使用错误的格式说明符printf是未定义的行为,这在7.19.6.1The fprintf function部分中进行了介绍,该部分还涵盖printf了关于格式说明符的内容:

If a conversion specification is invalid, the behavior is undefined.248)If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.

如果转换规范无效,则行为未定义。248)如果任何参数不是相应转换规范的正确类型,则行为未定义。

Update

更新

Visual Studiodoes not support the zformat specifier:

Visual Studio不支持z格式说明符

The hh, j, z, and t length prefixes are not supported.

不支持 hh、j、z 和 t 长度前缀。

the correct format specifier in this case would be %Iu.

在这种情况下,正确的格式说明符是%Iu.

回答by Fiddling Bits

The compiler is warning you that you may suffer a loss of precision. That is, the format specifier that you're using to print a sizeof, %d, is not capable of printing the full range of size_t. Change %dto %zuand your warning will go away.

编译器警告您可能会损失精度。也就是说,格式说明您使用打印sizeof%d是不是能够印刷的全方位的size_t。更改%d%zu,您的警告将消失。

回答by Suraj

i had the same problem in Linux. the same program runs without error in windows (means '%d' worked without error), but for linux i had to replace all the '%d' with'%lu' to run the program.

我在 Linux 中遇到了同样的问题。相同的程序在 Windows 中运行没有错误(意味着 '%d' 工作没有错误),但对于 linux,我必须用 '%lu' 替换所有的 '%d' 才能运行该程序。