473,395 Members | 1,348 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problem about type promotion and equality operator.

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.

Jun 6 '06 #1
2 2414
Le 06-06-2006, KOFKS <ge*****@gmail.com> a écrit*:
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?


Did you swap the two EOF values involved in the test ?
The EOF truncated value is the one returned by getchar().
Assume EOF == -1, and char are unsigned.
Then, when getchar returns EOF
'c = getchar()' <=> 'c = EOF' <=> 'c = -1'.
If char is unsigned and 8 bits, c == 255.
Then, when comparing 255 and -1, equality test fails.

Marc Boyer
Jun 6 '06 #2
> The EOF truncated value is the one returned by getchar().

Get it!
I do mis-understand something. I thought the truncation took place when
the operator
"!=" worked.
Maybe I should spend a little more time to think about it before
pasting anything here :)
Thank you very much!

Jun 6 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: David zhu | last post by:
I've got different result when comparing two strings using "==" and string.Compare(). The two strings seems to have same value "1202002" in the quick watch, and both have the same length 7 which I...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
6
by: Edward Diener | last post by:
Now that operator overloading allows to ref classes to be compared for equality using == syntax, how does one compare the actual ref pointers ( ^ ) for equality instead ? As an example: ...
6
by: KOFKS | last post by:
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...
3
by: Jing Yong | last post by:
I have a classtruct of over 50 members class A { public: type -- 50 members A(){initialize all members} friend bool operator==(A&,A&){//my operator goes here } }B;
3
by: toton | last post by:
Hi, I have a struct Point { int x, int y; } The points are stored in a std::vector<Pointpoints; (global vector) I want to add equality (operator == ) for the point, which will check equality...
16
by: EM.Bateman | last post by:
Working on Visual Studio .Net I've implemented a class: #ifndef CONTRIBUTOR_H #define CONTRIBUTOR_H enum Gender {male=1, female, unk}; #include <iostream> #include <iomanip> #include...
5
by: Peng Yu | last post by:
Hi, Please see the following code and the output. It seems that if one of %'s oprand is unsigned, the other will also be converted to unsigned. There is a operator precedence table in...
3
by: Zak | last post by:
I have some c++ source where construction via Type var(init); // compiles and... Type var = init; // does not I thought the two are supposed to be completely equivalent? Why does type...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.