type of character constants
Charlie Goldensher
charlie at vicorp.UUCP
Sat Mar 11 02:42:23 AEST 1989
In article <3711 at xyzzy.UUCP>, throopw at agarn.dg.com (Wayne A. Throop) writes:
> > scm at datlog.co.uk ( Steve Mawer )
> >> diamond at 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 at vicorp.uu.NET -- Charlie Goldensher
More information about the Comp.lang.c
mailing list