如何打印uint32_t和uint16_t变量的值? [英] How do I print uint32_t and uint16_t variables' value?

查看:168
本文介绍了如何打印uint32_t和uint16_t变量的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打印uint16_t和uint32_t值,但是它没有提供所需的输出.

I am trying to print an uint16_t and uint32_t value, but it is not giving the desired output.

#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;
}

我也用过

 printf("%"PRIu32, a);

显示错误.

如何打印这些值以及所需的输出是什么?

How do I print these values and what will be the desired output?

推荐答案

如果您想要 intN_t 的所有这些漂亮的新格式说明符,则需要包含 inttypes.h 类型及其兄弟,并且 是正确的(即可移植的)方法,前提是您的编译器符合C99.如果大小与您的想法不同,则不应使用%d %u 之类的标准字体.

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. You shouldn't use the standard ones like %d or %u in case the sizes are different to what you think.

它包括 stdint.h ,并用很多其他东西扩展了它,例如可以用于 printf/scanf 系列调用的宏.ISO C99标准的7.8节对此进行了介绍.

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. This is covered in section 7.8 of the ISO C99 standard.

例如,以下程序:

#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;
}

输出:

1234
5678

这篇关于如何打印uint32_t和uint16_t变量的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆