【发布时间】:2021-04-09 01:34:21
【问题描述】:
我必须从标准输入中准确读取 4 个字节,然后将其视为小端序并在标准输出上显示,但我不知道我读取的内容是否正好是 4 个字节。我试过这个:
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <byteswap.h>
int main() {
uint32_t f;
printf("Enter a value :");
scanf ("%" SCNu32, &f);
printf("Value of integer:%d", f);
//printf("Value of integer:%d", __bswap_32(f));
return 0;
}
我不确定这是否准确读取 4 个字节。我尝试了以下整数:
input->output
123->123
12345678->12345678
123456789->123456789
4294967295->-1
4294967294->-2
4294967296->-1
1->1
我认为通过打印f 会像这样:1234000(f=1234 的值)或12345678(f=123456789 的值)。
【问题讨论】:
-
%d打印一个签名int,试试%u。 -
我刚刚尝试过
%u和它一样。 -
你的意思是你还在得到
4294967295->-1?%u不能输出负值。目前还不是很清楚你要做什么。 -
对不起,你是对的!我尝试使用
123456789而不是4294967295。如果我尝试使用4294967296,我仍然会得到4294967295,我认为这是正确的。谢谢!