下面程序中的char变量data2如何存储整数值?
#include <stdio.h>
int main()
{
int data1 = 1230; //initialize data1 with 1230
char data2 = data1; //initialize data2 variable with data1 variable
printf("data2 = %X\n",data2); // print data2 variable
printf("data1 = %X\n",data1); // print data1 variable
printf("data2 size = %d\n",sizeof(data2)); //print size of data2 variable
return 0;
}
answer:
data2 = FFFFFFCE
data1 = FFFFFFCE
data2 size = 1
最佳答案
ce 04 00 00 -> data1 = 1230 it is data1's memory struct
data2=data1在程序集中
001C13A5 mov al,byte ptr [data1]
001C13A8 mov byte ptr [data2],al
所以data2的值是
ce
,这就是我们要排除的。但为什么我们在屏幕上看到
FFFFFFCE
?我想是因为printf
关于c - 如何在字符变量中存储整数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24504423/