c - c中如何存储整数和字符

标签 c char int interpretation

#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/

相关文章:

c - 区分扫描一个字符或多个字符

c++ - 用 int 值初始化 vector <char>

c# - 将 int 转换为 char 的最快方法是什么

php - 许多记录是 Varchar 还是 int?

c++ - 如何在excel中使用标准输入 '<'?

c++ - 使用 pThreads,是否可以编写一个函数来检测从哪个线程调用它?

c - 什么是字节?

c - AVR-GCC 中 undefined reference

c - 如何在特定 IP 上打开套接字

c - 为什么足够了? (将 int 存储在 char 数组中)