简体   繁体   English

我无法弄清楚下面的代码是如何产生ffffffaa的输出的,请帮助我理解吗?

[英]i am unable to figure out how the below code produce output as ffffffaa please help me understand?

Hello i am unable to figure out how the below code produce output as ffffffaa please help me understand 您好,我无法弄清楚以下代码如何将输出作为ffffffaa,请帮助我理解

#include<stdio.h>
 int main()
{
  int a=0xaaaaaaaa;
  char *p=(char*)&a;
  printf("%x\n",*p);
}  

Variadic functions like printf perform the default argument promotion on its trailing arguments. 诸如printf之类的可变函数会对其尾随参数执行默认参数提升 A char is promoted to an int and when char is signed on the platform a sign extension is performed. char提升为int并在平台上对char进行签名时,将执行符号扩展。

To avoid the sign extension use unsigned char types or cast *p to unsigned char in the printf call of your program. 为避免符号扩展名,请在程序的printf调用中使用unsigned char类型或将*punsigned char

I'm unable to figure out how the code above can produce ffffffaa because it cannot do that with a "%d" specifier ;-). 我无法弄清楚上面的代码如何产生ffffffaa因为它不能使用"%d"说明符;-)。

You probably mistyped you code sample and it should read: 您可能输入了错误的代码示例,它应显示为:

int main() {
    int a=0xaaaaaaaa;
    char *p=(char*)&a;
    printf("%x\n",*p);
}  

For this code to produce ffffffaa , char must be 8 bit signed, and int must be stored in little endian order or have a size of at most 32 bits. 为了使该代码产生ffffffaa ,char必须为8位带符号,并且int必须以小端顺序存储或最大为32位。

&a is the address of the first byte of a in memory. &a是第一字节的地址a在存储器中。 Dereferencing this address as a char * loads one byte and sign extends it as an int before passing it to printf as this function is variadic. 将这个地址取消引用为char *会加载一个字节,并在将其传递给printf之前将符号扩展为int ,因为此函数是可变参数。 Extra arguments to variatic functions with types smaller than int are promoted to int and passed as such, and floating point types smaller than double are promoted to double and passed as such. 类型小于int变量函数的额外参数被提升为int并按原样传递,小于double浮点类型被提升为double并按原样传递。 printf receives an int but prints the hexadecimal representation of an unsigned int for the format specifier %x . printf接收一个int但打印格式为%xunsigned int的十六进制表示形式。 It's a good thing int and unsigned int are passed the same way ;-) intunsigned int以相同的方式传递是一件好事;-)

Since the value 0xaaaaaaaa has the same representation in little endian and big endian order, your code will not depend on endianness as long as int is 32 bits or less. 由于值0xaaaaaaaa在小端和大端顺序中具有相同的表示形式,因此只要int为32位或更少,您的代码就不会依赖于字节序。 But on a machine with 64 bit int in big endian order (such as some late PowerPCs), the actual memory layout of a would be 00 00 00 00 aa aa aa aa . 但是,随着64位的机器上int在大端顺序(如有些晚的PowerPC),实际内存布局a00 00 00 00 aa aa aa aa Your code would then produce 0 output. 您的代码将产生0输出。

You can learn about endianness with this code sample: 您可以通过以下代码示例了解字节序:

#include <stdio.h>    
int main(void) {
    int i, a = 0x12345678;
    unsigned char *p = (unsigned char *)&a;
    printf("Memory layout for %x is:", a);
    for (i = 0; i < (int)sizeof(a); i++)
        printf(" %02x", p[i]);
    printf("\n");
}

Different compilers may produce different output on the same machine if they target a different architecture (32 vs 64 bits). 如果不同的编译器针对不同的体系结构(32位与64位),它们可能会在同一台机器上产生不同的输出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 请帮我找出malloc问题 - Please help me figure out malloc issue 在下面的程序中,我期望无限循环作为输出。 但是输出是0,请帮我解释一下它背后的概念 - In the below program I was expected Infinite loop as a output. But output is 0, please help me with explain the concept behind it 有人可以帮助我了解此代码的输出 - Can someone help me to understand output of this code 获取以下代码的细分错误。 我无法弄清楚我要去哪里 - Getting segmentation fault for the code below. I am not able to figure out where am I going wrong 有人可以帮我弄清楚为什么我收到错误 malloc(): corrupted top size - Could someone help me figure out why I am getting the error malloc(): corrupted top size 本练习来自 w3resource。 请帮我弄清楚主要代码中charPermu的工作,尤其是指针参数 - This excercise is from w3resource. Help me to figure out what charPermu's job in the main code especially the pointer parameter, please 谁能帮我解决我在vs代码中遇到的这个错误? - can anyone please help me solving this error that i am facing in vs code? 谁能给我解释一下这个 function,我无法理解 - Could anyone please explain me this function, I am not able to understand this 你能帮我理解指针吗? - Could you help me understand Pointers please? 我正在尝试这个多线程代码来理解。 谁能帮助我理解“打印消息”部分。 我在此发布代码 - i am trying this multi threaded code to understand. can anyone help me understand the “print message” part. i am posting the code herewith
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM