Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

c - Type of character constant

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 pre?xed 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-de?ned 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-de?ned. 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...