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

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

问题描述

我要打印uint16_t和uint32_t的价值,但它是不会放弃希望的O / P。

I am trying to print uint16_t and uint32_t value but it is not giving desired o/p.

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

其示出一个错误。

which is showing an error.

如何打印这些值,这将是所需的O / P

How to print these value and what will be the desired o/p

推荐答案

您需要包括 inttypes.h 如果你想为所有那些漂亮的新格式说明 intN_t 的类型和他们的弟兄,而的正确的(即便携式)的方式来做到这一点,只要你的编译器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天全站免登陆