我收到警告 warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat]
我正在编写一个非常基本的程序,它提供数据类型的大小,但在 Linux 环境中我收到此警告,而在 Visual Studio 中,该程序在没有任何警告的情况下运行。源代码如下:-
#include<stdio.h>
int main()
{
int a;
printf("\nThe Size Of Integer A Is = \t%d", sizeof(a));
return 0;
}
答案将不胜感激,而且任何人都可以告诉我解决此类警告的正确方法,因为我是这个 C 的新手并且它是标准的。
sizeof
返回 size_t
类型。使用 %zu
打印 sizeof
值的说明符.
printf("\nThe Size Of Integer A Is = \t%zu", sizeof(a));
C11 6.5.3.4 sizeof
和 _Alignof
运营商:
5 The value of the result of both operators is implementation-defined, and its type (an
unsigned integer
type) is size_t
, defined in <stddef.h>
(and other headers).
注意:作为loreb在他的 comment 中指出当您在 Windows 中编译代码时,您很可能会收到如下警告:
[Warning] unknown conversion type character 'z' in format [-Wformat]
[Warning] too many arguments for format [-Wformat-extra-args]