简体   繁体   English

如何打印 uint32_t 和 uint16_t 变量的值?

[英]How do I print uint32_t and uint16_t variables' value?

I am trying to print an uint16_t and uint32_t value, but it is not giving the desired output.我正在尝试打印 uint16_t 和 uint32_t 值,但它没有提供所需的输出。

#include <stdio.h>
#include <netinet/in.h>

int main()
{
    uint32_t a = 12, a1;
    uint16_t b = 1, b1;
    a1 = htonl(a);
    printf("%d---------%d", a1);
    b1 = htons(b);
    printf("\n%d-----%d", b, b1);
    return 0;
}

I also used我也用过

 printf("%"PRIu32, a);

which is showing an error.这是显示错误。

How do I print these values and what will be the desired output?如何打印这些值以及所需的输出是什么?

You need to include inttypes.h if you want all those nifty new format specifiers for the intN_t types and their brethren, and that is the correct (ie, portable) way to do it, provided your compiler complies with C99.如果您想要intN_t类型及其兄弟的所有那些漂亮的新格式说明符,您需要包含inttypes.h ,并且这正确的(即,可移植的)方法,前提是您的编译器符合 C99。 You shouldn't use the standard ones like %d or %u in case the sizes are different to what you think.你不应该使用标准的%d%u以防大小与你的想法不同。

It includes stdint.h and extends it with quite a few other things, such as the macros that can be used for the printf/scanf family of calls.它包括stdint.h并扩展了很多其他东西,例如可用于printf/scanf系列调用的宏。 This is covered in section 7.8 of the ISO C99 standard.这在 ISO C99 标准的第 7.8 节中有介绍。

For example, the following program:例如,以下程序:

#include <stdio.h>
#include <inttypes.h>
int main (void) {
    uint32_t a=1234;
    uint16_t b=5678;
    printf("%" PRIu32 "\n",a);
    printf("%" PRIu16 "\n",b);
    return 0;
}

outputs:输出:

1234
5678

The macros defined in <inttypes.h> are the most correct way to print values of types uint32_t , uint16_t , and so forth -- but they're not the only way. <inttypes.h>中定义的宏是打印uint32_tuint16_t等类型值的最正确方法——但它们不是唯一的方法。

Personally, I find those macros difficult to remember and awkward to use.就我个人而言,我发现这些宏难以记忆且难以使用。 (Given the syntax of a printf format string, that's probably unavoidable; I'm not claiming I could have come up with a better system.) (考虑到printf格式字符串的语法,这可能是不可避免的;我并不是说我可以想出一个更好的系统。)

An alternative is to cast the values to a predefined type and use the format for that type.另一种方法是将值转换为预定义的类型并使用该类型的格式。

Types int and unsigned int are guaranteed by the language to be at least 16 bits wide, and therefore to be able to hold any converted value of type int16_t or uint16_t , respectively.语言保证intunsigned int类型至少为 16 位宽,因此能够分别保存int16_tuint16_t类型的任何转换值。 Similarly, long and unsigned long are at least 32 bits wide, and long long and unsigned long long are at least 64 bits wide.类似地, longunsigned long至少为 32 位宽, long longunsigned long long至少为 64 位宽。

For example, I might write your program like this (with a few additional tweaks):例如,我可能会像这样编写您的程序(还有一些额外的调整):

#include <stdio.h>
#include <stdint.h>
#include <netinet/in.h>  

int main(void)
{
    uint32_t a=12, a1;
    uint16_t b=1, b1;
    a1 = htonl(a);
    printf("%lu---------%lu\n", (unsigned long)a, (unsigned long)a1);
    b1 = htons(b);
    printf("%u-----%u\n", (unsigned)b, (unsigned)b1);
    return 0;
}

One advantage of this approach is that it can work even with pre-C99 implementations that don't support <inttypes.h> .这种方法的一个优点是它甚至可以与不支持<inttypes.h>的 C99 之前的实现一起工作。 Such an implementation most likely wouldn't have <stdint.h> either, but the technique is useful for other integer types.这样的实现很可能也没有<stdint.h> ,但该技术对其他整数类型很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM