为什么是C字符文字整数,而不是字符? [英] Why are C character literals ints instead of chars?

查看:101
本文介绍了为什么是C字符文字整数,而不是字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,的sizeof('A')==的sizeof(char)的== 1 。这使得直观的感觉,因为'A'是一个字符,并和的sizeof(char)的== 1 定义该标准。结果

In C++, sizeof('a') == sizeof(char) == 1. This makes intuitive sense, since 'a' is a character literal, and sizeof(char) == 1 as defined by the standard.

在C然而,的sizeof('A')==的sizeof(INT)。也就是说,它似乎将c字符文字实际上是整数。有谁知道为什么吗?我可以找到大量的提到了这款C怪癖,但它为什么不存在任何解释。

In C however, sizeof('a') == sizeof(int). That is, it appears that C character literals are actually integers. Does anyone know why? I can find plenty of mentions of this C quirk but no explanation for why it exists.

推荐答案

同一主题

更具体的积分促销在K&安培; R C这几乎是(?)
  不可能用一个字符值没有它被提升为第一为int,
  在第一个地方,以便使字符常量INT消除了一步。
  有,现在仍然是多字符常量,如ABCD或者不管
  许多人会适合用int。

"More specifically the integral promotions. In K&R C it was virtually (?) impossible to use a character value without it being promoted to int first, so making character constant int in the first place eliminated that step. There were and still are multi character constants such as 'abcd' or however many will fit in an int."

这篇关于为什么是C字符文字整数,而不是字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆