[英]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
类型或将*p
为unsigned 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
但打印格式为%x
的unsigned int
的十六进制表示形式。 It's a good thing int
and unsigned int
are passed the same way ;-) int
和unsigned 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),实际内存布局a
是00 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.