Path: utzoo!utgpu!utstat!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!leah!itsgw!steinmetz!uunet!vicorp!charlie From: charlie@vicorp.UUCP (Charlie Goldensher) Newsgroups: comp.lang.c Subject: Re: type of character constants Summary: Why should we care? Keywords: character constants Message-ID: <1644@vicorp.UUCP> Date: 10 Mar 89 16:42:23 GMT References: <13068@steinmetz.ge.com> <102@servio.UUCP> <10138@socslgw.csl.sony.JUNET> <1783@dlvax2.datlog.co.uk> <3711@xyzzy.UUCP> Reply-To: charlie@vicorp.UUCP (Charlie Goldensher) Followup-To: comp.lang.c Organization: V. I. Corporation, Amherst, Massachusetts Lines: 37 In article <3711@xyzzy.UUCP>, throopw@agarn.dg.com (Wayne A. Throop) writes: > > scm@datlog.co.uk ( Steve Mawer ) > >> diamond@diamond. (Norman Diamond) > >> When you assign 'x' to a character, you are assigning an int to a > >> character. The reader knows that the type mismatch was intentional. > > Not if he knows the C language. A single character written within > > single quotes is a *character constant*. This isn't an int. > > Ha. A lot Steve knows. From K&R, 1st ed, pg 185 > > Character constants have type int; floating constants are double. > The paragraph from which this is excerpted is as follows: A constant is a primary expression. Its type may be int, long, or double depending on its form. Character contants have type int; floating constants are type double. On the previous page (page 184, 6.6 Arithmetic conversions) K&R say: First, any operands of type char or short are converted to type int, and any of type float are converted to double. So what does it matter if a character constant is of type char or of type int? If it is of type char, it will be *converted* to type int in any expression in which it is used. And if it is of type int it will be implicitly cast to type char if it assigned to a variable of type char. I don't care how the compiler writer chooses to implement it internally as long as they follow the appropriate conversion rules. Is there some reason I *should* care? -- charlie@vicorp.uu.NET -- Charlie Goldensher