“%d个”预计类型'诠释'的说法,但参数2的类型为'长unsigned int类型'[-Wformat =] [英] '%d' expects argument of type 'int', but argument 2 has type 'long unsigned int' [-Wformat=]

查看:1774
本文介绍了“%d个”预计类型'诠释'的说法,但参数2的类型为'长unsigned int类型'[-Wformat =]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是收到编译警告,但我不知道如何解决它:

 '%D'预计类型'诠释'的说法,但参数2的类型为'长期无符号整型[

该程序运行正常,但我仍然得到编译警告:

  / * Sizeof.c  - 计划告诉C变量的字节大小* /
#包括LT&;&stdio.h中GT;诠释主要(无效){
    的printf(\\ nA的字符为%d字节的sizeof(字符));
    的printf(\\楠int是%d字节的sizeof(INT));
    的printf(\\ nA的短为%d字节的sizeof(短));
    的printf(\\钠长为%d字节的sizeof(长));
    的printf(\\ nA的很长很长为%d字节\\ n,sizeof的(久长));
    的printf(\\南无符号的字符为%d字节的sizeof(无符号字符));
    的printf(\\楠unsigned int类型为%d字节的sizeof(无符号整数));
    的printf(\\南无符号短为%d字节的sizeof(无符号短));
    的printf(\\南无符号长为%d字节的sizeof(无符号长));
    的printf(\\南无符号加长为%d字节\\ n
            的sizeof(无符号很长很长));
    的printf(\\ nfloat为%d字节的sizeof(浮动));
    的printf(\\ nA的双为%d字节\\ n的sizeof(双));
    的printf(\\ nA的长双精度为%d字节\\ n,sizeof的(长双));    返回0;}


解决方案

的sizeof 的回报,你需要使用为size_t %祖的格式字符串,而不是%d个。该类型的无符号整数为size_t 可以变化(取决于平台),可能不是的长unsigned int类型的无处不在,这是包括在C99标准草案 6.5.3.4 sizeof操作符的段落的 4 的:


  的结果的值是实现定义,并且它的类型(无符号整型)
  是为size_t,定义(和其他头)。


另外请注意,使用错误的格式说明的printf 是不确定的行为,这是覆盖在部分 7.19.6.1 fprintf函数的,其中也包括的printf 关于格式说明说:


  

如果一个转换说明是无效的,其行为是不确定的。 248)如果任何参数类型不正确的相应转换规范,行为是不确定的。


更新

的Visual Studio 不支持以Z 格式说明


  

的HH,J,Z和T长度prefixes不被支持。


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

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;

}

解决方案

sizeof returns size_t you need to use %zu for the format string instead of %d. The type of unsigned integer of size_t can vary (depending on platform) and may not be long unsigned int everywhere, which is covered in the draft C99 standard section 6.5.3.4 The sizeof operator paragraph 4:

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

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

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.

Update

Visual Studio does not support the z format specifier:

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

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

这篇关于“%d个”预计类型'诠释'的说法,但参数2的类型为'长unsigned int类型'[-Wformat =]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆