c - 使用 SizeOf 时收到警告

标签 c sizeof

<分区>

我收到警告 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]

关于c - 使用 SizeOf 时收到警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21144780/

相关文章:

python - python对象和类的内存布局是怎样的?

c++ - 空洞的大小是多少?

c++ - gcc - 将多个 c 文件链接到一个 c++ 文件

c - 如何告诉gcc不要在堆栈上对齐函数参数?

c - 没有printf的C中的"Hello world"?

c - 编写一个程序以继续处理整数直到素数

c++ - 具有不同消息大小的多播性能

C:非 NUL 终止的字符数组

c++ - 为什么这个数组的 sizeof() 是非法的?

c - 使用 sizeof 查找 argv 中字符串的大小