问题描述
我正在尝试打印 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("
%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 "
",a);
printf("%" PRIu16 "
",b);
return 0;
}
输出:
1234
5678
这篇关于如何打印 uint32_t 和 uint16_t 变量的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!