gpt4 book ai didi

c - C 中的 Printf 打印 fffffffe1 而不是 e1

转载 作者:行者123 更新时间:2023-11-30 19:57:18 25 4
gpt4 key购买 nike

我很困惑,我有一个小程序,我在不同的地址位置打印值。

int main ()
{
// unsigned int x = 0x15711056;
unsigned int x = 0x15b11056;
char *c = (char*) &x;

printf ("*c is: 0x%x\n", *c);
printf("size of %d\n", sizeof(x));
printf("Value at first address %x\n", *(c+0));
printf("Value at second address %x\n", *(c+1));
printf("Value at third address %x\n", *(c+2));
printf("Value at fourth address %x\n", *(c+3));

对于注释的 unsigned int x,printf 值符合预期,即

  printf("Value at first address %x\n", *(c+0)) = 56
printf("Value at second address %x\n", *(c+1))= 10
printf("Value at third address %x\n", *(c+2))= 71
printf("Value at fourth address %x\n", *(c+3))= 15

但是对于未注释的 int x 为什么我得到的结果低于 *(c+2) 它应该是 b1 而不是 ffffffb1。请帮助我理解这一点,我正在在线 IDE 上运行它 https://www.onlinegdb.com/online_c_compiler 。我的电脑是 i7 intel。

  printf("Value at first address %x\n", *(c+0)) = 56
printf("Value at second address %x\n", *(c+1))= 10
printf("Value at third address %x\n", *(c+2))= ffffffb1
printf("Value at fourth address %x\n", *(c+3))= 15

最佳答案

该值的符号为0xB1,二进制为10110001,您需要使用unsigned char指针:

unsigned char *c = (unsigned char*) &x;

您的代码适用于 0x7F 之前的任何字节。

关于c - C 中的 Printf 打印 fffffffe1 而不是 e1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48482596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com