C语言 如何在 C 中打印 int 的大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5943840/
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 08:36:52  来源:igfitidea点击:

How do I print the size of int in C?

c

提问by Jimm

I am trying to compile the below on RHEL 5.6 , 64 bit, and i keep getting a warning

我正在尝试在 RHEL 5.6 64 位上编译以下内容,但我不断收到警告

"var.c:7: warning: format ‘%d' expects type ‘int', but argument 2 has type ‘long unsigned int'"

“var.c:7: 警告:格式 '%d' 需要类型 'int',但参数 2 的类型为 'long unsigned int'”

Here is my code:

这是我的代码:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    unsigned int n =10;
    printf("The size of integer is %d\n", sizeof(n));
}

It does not matter if i change the declaration for "n" to following

我是否将“n”的声明更改为以下无关紧要

  1. signed int n =10;
  2. int n = 10;
  1. 有符号整数 n = 10;
  2. 整数 n = 10;

All i want to do is print the size of integer on my machine, without really looking into limits.h.

我想要做的就是在我的机器上打印整数的大小,而无需真正查看limits.h。

回答by Robert Groves

The sizeof function returns a size_ttype. Try using %zuas the conversion specifier instead of %d.

sizeof 函数返回一个size_t类型。尝试使用%zu作为转换说明符而不是%d.

printf("The size of integer is %zu\n", sizeof(n));

To clarify, use %zuif your compiler supports C99; otherwise, or if you want maximum portability, the best way to print a size_tvalue is to convert it to unsigned longand use %lu.

澄清%zu一下,如果您的编译器支持 C99 ,请使用;否则,或者如果你想最大的可移植性,打印的最佳方法size_t值将其转换为unsigned long和使用%lu

printf("The size of integer is %lu\n", (unsigned long)sizeof(n));

The reason for this is that the size_tis guaranteed by the standard to be an unsigned type; however the standard does not specify that it must be of any particular size, (just large enough to represent the size of any object). In fact, if unsigned long cannot represent the largest object for your environment, you might even need to use an unsigned long long cast and %lluspecifier.

这样做的原因是size_t标准保证 是无符号类型;然而,该标准并没有规定它必须是任何特定的大小,(只要大到足以代表任何对象的大小)。事实上,如果 unsigned long 不能代表您环境中的最大对象,您甚至可能需要使用 unsigned long long 类型转换和%llu说明符。

In C99 the z length modifier was added to provide a way to specify that the value being printed is the size of a size_t type. By using %zuyou are indicating the value being printed is an unsigned value of size_tsize.

在 C99 中,添加了 z 长度修饰符以提供一种方法来指定正在打印的值是 size_t 类型的大小。通过使用,%zu您表示正在打印的值是size_t大小的无符号值。

This is one of those things where it seems like you shouldn't have to think about it, but you do.

这是您似乎不必考虑的事情之一,但您确实这样做了。

Further reading:

进一步阅读:

回答by Aurojit Panda

Your problem is that size_t is an unsigned type. Try using

您的问题是 size_t 是无符号类型。尝试使用

printf("The size of integer is %u\n", sizeof(n));

and you should get rid of that warning.

你应该摆脱那个警告。

回答by SIFE

I think you should write this instead:

我认为你应该写这个:

printf("The size of integer is %d\n", sizeof(int));