字符常量的类型 [英] Type of character constant

查看:133
本文介绍了字符常量的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习物理和在我们的编程课程的讲义上写着一个字符常量,在C有键入字符,其中由字符常量我的意思是前pression像'X'
问我,如果讲师,这是一个错误后,他说没有。显示他的C90,C99和C11,它是写得很清楚,一个字符常量的类型后的内部他还是不说,这是一个错误。

于是问他前再次我想,以确保自己,我得到了正确的方式,为什么它是这样的,因为它似乎是浪费内存。
一切,我发现了它为什么是这样,就是它是因为历史的原因,这是相当模糊的。
此外,我想知道为什么在C ++中,他们改变一个字符常量为字符

类型

编辑:非常感谢您的答案。


解决方案

在线C语言标准,2011 草案:


6.4.4.4字符常量的结果
...结果
2的整数字符常量是括一个或多​​个多字节字符序列
在单引号中,如'X'。宽字符常量是一样的,除了由固定的pre网络
U U 。除少数例外以后详述的,序列中的元素是
源代码字符集的任何成员;它们被映射定义一个实现德音响
地执行字符集的成员。结果
...结果
10的整数字符常量的类型 INT 。整型字符常量的值
包含映射到单字节执行字符一个字符是
PTED为整数的映射字符间$ P $重新presentation的数值。
包含多个字符的整型字符常量的值(例如,
'AB'),或包含字符或转义序列不映射到一个单字节
执行字符,是实现德网络定义。如果一个整数字符常量包含
单个字符或转义序列,它的价值是导致一当一个对象
键入字符,其价值在于单个字符或转义序列转换为
键入 INT

所以,是的,在C,单字符常量,如'X'具有类型 INT

<罢工>的为什么的是这种情况下,据我所知,在很大程度上失去了历史 1 ,但我怀疑这是为了最大限度地减少对结果进行比较时转换的getchar (返回一个 INT )或类似的printf (将自动推动型的前pressions 字符 INT )。

由于C ++的I / O和类型一般操作提供的备用机制,这是有意义的字符常量有键入字符

这有关系的在实践中的?不是在我的经验,但我在文本处理的经验是不是丰富。

我其实应该读什么我从标准的复制;其原因是在该片段给出。你可以多字符常量像'ABC',这不会在逻辑映射到单个字符值。



<罢工> 1。现在看sometbody找到里奇或Kernighan的解释为什么他们这样做是完全是一个广泛引用

I am studying physics and in the lecture notes of our programming course it is written that a character constant, in C has type char, where by character constant I mean an expression like 'x'. After asking my lecturer if that was a mistake he said no. After showing him the C90, C99 and C11, where it is clearly written that a character constant has type int he still didn't say it was a mistake.

So before asking him again I wanted to assure myself that I got it the right way, and why it is that way, because it seems like a waste of memory. Everything I found out about why it is that way, is that it is because of historic reasons, which is rather vague. Also I would like to know why in C++ , they changed the type of a character constant to char.

EDIT: thanks a lot for the answers.

解决方案

Online C language standard, 2011 draft:

6.4.4.4 Character constants
...
2 An integer character constant is a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x'. A wide character constant is the same, except prefixed by the letter L, u, or U. With a few exceptions detailed later, the elements of the sequence are any members of the source character set; they are mapped in an implementation-defined manner to members of the execution character set.
...
10 An integer character constant has type int. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer. The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined. If an integer character constant contains a single character or escape sequence, its value is the one that results when an object with type char whose value is that of the single character or escape sequence is converted to type int.

So yes, in C, single character constants such as 'x' have type int.

Why this is the case is, AFAIK, largely lost to history1, although I suspect it was to minimize conversions when comparing against the results of getchar (which returns an int) or working with variadic functions like printf (which would automatically promote any expressions of type char to int).

Since C++ provided alternate mechanisms for I/O and type-generic operations, it made sense for character constants to have type char.

Does this matter in practice? Not in my experience, but my experience in text processing isn't that extensive.

I should actually read what I copy from the standard; the reason is given in that snippet. You can have multi-character constants like 'abc', which would not logically map to a single char value.


1. Now watch sometbody find an extensive quote from Ritchie or Kernighan explaining exactly why they did it

这篇关于字符常量的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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