#include<stdio.h>
void main()
{
int a=65;
char d='A';
if(a==d)
printf("both are same");
}
输出是相同的。这里 a 是一个整数,所以 65 存储在 32 位中,d 是一个字符,存储在 8 位中,它们怎么可能与计算机相同,所有操作都转换为二进制。
最佳答案
计算机能够在二进制级别上比较 char 与 int,因为 Implicit type promotion rules .
If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions.
这意味着在处理器对两者进行比较之前,您的 char 会被提升为 int
。
关于c - c中如何存储整数和字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50835423/