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

时间:2022-09-01 14:31:55

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. signed int n = 10;
  3. int n = 10;
  4. int n = 10;

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

我想要做的就是在我的机器上打印整数的大小,而不是真正关注limits.h。

3 个解决方案

#1


34  

The sizeof function returns a size_t type. Try using %zu as the conversion specifier instead of %d.

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

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

To clarify, use %zu if your compiler supports C99; otherwise, or if you want maximum portability, the best way to print a size_t value is to convert it to unsigned long and use %lu.

为了澄清,如果您的编译器支持C99,请使用%zu;否则,或者如果您想要最大的可移植性,打印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_t is 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 %llu specifier.

原因是size_t由标准保证为无符号类型;但是标准没有规定它必须具有任何特定的大小(只要大到足以表示任何对象的大小)。实际上,如果unsigned long不能代表您环境的最大对象,您甚至可能需要使用unsigned long long cast和%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 %zu you are indicating the value being printed is an unsigned value of size_t size.

在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:

进一步阅读:

#2


3  

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.

你应该摆脱那个警告。

#3


-3  

I think you should write this instead:

我想你应该写这个:

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

#1


34  

The sizeof function returns a size_t type. Try using %zu as the conversion specifier instead of %d.

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

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

To clarify, use %zu if your compiler supports C99; otherwise, or if you want maximum portability, the best way to print a size_t value is to convert it to unsigned long and use %lu.

为了澄清,如果您的编译器支持C99,请使用%zu;否则,或者如果您想要最大的可移植性,打印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_t is 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 %llu specifier.

原因是size_t由标准保证为无符号类型;但是标准没有规定它必须具有任何特定的大小(只要大到足以表示任何对象的大小)。实际上,如果unsigned long不能代表您环境的最大对象,您甚至可能需要使用unsigned long long cast和%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 %zu you are indicating the value being printed is an unsigned value of size_t size.

在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:

进一步阅读:

#2


3  

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.

你应该摆脱那个警告。

#3


-3  

I think you should write this instead:

我想你应该写这个:

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