Problem about type promotion and equality operator.

K

KOFKS

I have come across a problem when I'm reading "comp.lang.c FAQ list ·
Question 12.1".
This item is about
char c;
while((c = getchar()) != EOF) ...

One paragraph of the item writes:"If type char is unsigned, an actual
EOF value will be truncated (by having its higher-order bits discarded,
probably resulting in 255 or 0xff) and will not be recognized as EOF,
resulting in effectively infinite input. "

However, in the C99 standard 6.5.9 Equality operators Semantics, I see
the following:
"...Any two values of arithmetic types from different type domains are
equal if and only if the results of their conversions to the (complex)
result type detemined by the usual arithmetic conversions are equal."

As I know, the usual arithmetic conversion of char is that it will be
converted to int during the integral promotion process. Why does EOF be
truncated other than the char variable be promoted by zero extension at
the case mentioned above?

So there must be something wrong. Am I misunderstanding something?
Can anyone help?
TIA.
 
F

Flash Gordon

KOFKS wrote:

First of all, please try to avoid double posts.
I have come across a problem when I'm reading "comp.lang.c FAQ list ·
Question 12.1".
This item is about
char c;
while((c = getchar()) != EOF) ...

One paragraph of the item writes:"If type char is unsigned, an actual
EOF value will be truncated (by having its higher-order bits discarded,
probably resulting in 255 or 0xff) and will not be recognized as EOF,
resulting in effectively infinite input. "

However, in the C99 standard 6.5.9 Equality operators Semantics, I see
the following:
"...Any two values of arithmetic types from different type domains are
equal if and only if the results of their conversions to the (complex)
result type detemined by the usual arithmetic conversions are equal."

As I know, the usual arithmetic conversion of char is that it will be
converted to int during the integral promotion process. Why does EOF be
truncated other than the char variable be promoted by zero extension at
the case mentioned above?

So there must be something wrong. Am I misunderstanding something?
Can anyone help?
TIA.

First look at the sub-expression (c = getchar()). Obviously, the value
returned by getchar has to be converted to type char to be assigned to
c. If c is unsigned, then this will obviously result in a positive
value, since negative values won't fit in to an unsigned variable! The
result of the expression is the value assigned to c rather than the
value returned by getchar. So, when you do the next bit, comparing it to
EOF, that positive value gets converted to type int, but the value is
not changed, it stays positive. So as EOF is negative and the result of
(C = getchar()) is positive, they will never be the same.

You can also hit problems if char is signed, since EOF could be outside
the range of char (e.g. -256 on an implementation with an 8 bit signed
char).

Note, to the pedants, I'm deliberately ignoring systems where
sizeof(int)==1.
 
K

KOFKS

Flash said:
First of all, please try to avoid double posts.
I'm very sorry for some problem of my explorer, I repost the same
content for a second time. But I removed it as soon as I found it.
Thank you for your expalination and reminding me of double posts.
 
C

CBFalconer

KOFKS said:
I'm very sorry for some problem of my explorer, I repost the same
content for a second time. But I removed it as soon as I found it.
Thank you for your expalination and reminding me of double posts.

You can't "remove it". Once it has been sent it goes into the
general maw of the usenet system (which is *not* google), and that
system generally will not accept any "removal" attempts. Allowing
such would permit evildoers to totally foul the system.

You would be much better off getting a real newsreader and
connecting to a news server. Most ISPs provide one for free. A
real newsreader, available free, is Thunderbird, available from
mozilla.org. Don't use Outlook anything, or anything from
Microsoft. They are flawed.

--
"Our enemies are innovative and resourceful, and so are we.
They never stop thinking about new ways to harm our country
and our people, and neither do we." -- G. W. Bush.
"The people can always be brought to the bidding of the
leaders. All you have to do is tell them they are being
attacked and denounce the pacifists for lack of patriotism
and exposing the country to danger. It works the same way
in any country." --Hermann Goering.
 
K

Keith Thompson

CBFalconer said:
You can't "remove it". Once it has been sent it goes into the
general maw of the usenet system (which is *not* google), and that
system generally will not accept any "removal" attempts. Allowing
such would permit evildoers to totally foul the system.
[...]

Actually, it is possible to (attempt to) cancel a Usenet message. At
times evildoers have used this facility to foul the system, which is
why many sites don't honor cancel messages (except perhaps from
recognized spam cancellers).
 
C

CBFalconer

Keith said:
CBFalconer said:
You can't "remove it". Once it has been sent it goes into the
general maw of the usenet system (which is *not* google), and that
system generally will not accept any "removal" attempts. Allowing
such would permit evildoers to totally foul the system.
[...]

Actually, it is possible to (attempt to) cancel a Usenet message. At
times evildoers have used this facility to foul the system, which is
why many sites don't honor cancel messages (except perhaps from
recognized spam cancellers).

Isn't that what I said?

--
"Our enemies are innovative and resourceful, and so are we.
They never stop thinking about new ways to harm our country
and our people, and neither do we." -- G. W. Bush.
"The people can always be brought to the bidding of the
leaders. All you have to do is tell them they are being
attacked and denounce the pacifists for lack of patriotism
and exposing the country to danger. It works the same way
in any country." --Hermann Goering.
 
K

Keith Thompson

CBFalconer said:
Keith said:
CBFalconer said:
KOFKS wrote:
Flash Gordon wrote:

First of all, please try to avoid double posts.

I'm very sorry for some problem of my explorer, I repost the same
content for a second time. But I removed it as soon as I found it.
Thank you for your expalination and reminding me of double posts.

You can't "remove it". Once it has been sent it goes into the
general maw of the usenet system (which is *not* google), and that
system generally will not accept any "removal" attempts. Allowing
such would permit evildoers to totally foul the system.
[...]

Actually, it is possible to (attempt to) cancel a Usenet message. At
times evildoers have used this facility to foul the system, which is
why many sites don't honor cancel messages (except perhaps from
recognized spam cancellers).

Isn't that what I said?

I didn't think it was quite the same thing, but it's no big deal.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top