type of character constants
Steve Mawer
scm at datlog.co.uk
Wed Mar 15 18:08:00 AEST 1989
First let me start by apologising for the content of my misguided
contribution to the character constant type/size debate. I had always
interpreted the meaning of character constant as 'constant of type char(acter)'
and have used expressions of the form 'x' whenever I've need to define
characters. In a lot of other code I've seen (not influenced by me!)
others appear to share my (incorrect) assumptions.
Thanks to the responses I've received, I believe I now fully understand
the concept of character constants. Where I've been bitten in the past
is when I've used them to define 8 bit characters (e.g. for PC code sets)
and have found that few compilers I've used believe e.g. (0200 == '\200').
I'd always thought this was due to the 8 bit value '\200' being widened
to an int for the comparison and having (optional) sign extension done.
However, many compilers didn't even believe (0200 == (unsigned)'\200').
I now realise this must be due to the \200 being the 8 bit value and the
inclusion of the surrounding quotes causing the widening, leaving the value
(optionally) sign extended at that time.
That's cleared that up (or so I thought). But then ..
in article <766 at twwells.uucp> bill at twwells.UUCP (T. William Wells) writes:
>
>There is a subtle difference. If 'c' is an integer constant, '\377'
>represents the value 255. If, on the other hand, it is a char
>constant, and characters sign extend, it represents -1.
>
As noted above, most compilers I've used believe that '\377' *is* -1; in
fact the only one I can recall that makes it 255 is that supplied with AIX.
So what is the 'correct' value for '\377', 255 or -1?
--
Steve C. Mawer <scm at datlog.co.uk> or < {backbone}!ukc!datlog!scm >
Voice: +44 1 863 0383 (x2153)
More information about the Comp.lang.c
mailing list