473,545 Members | 2,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 misunderstandin g something?
Can anyone help?
TIA.

Jun 6 '06 #1
2 2424
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 misunderstandin g 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
9491
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 have tried to print out by debug.writeline(). But the "==" operator results false, and string.Compare() results true. Somebody helps me!
0
3916
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. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header,...
6
4487
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: SomeRefObject ^ obj1(..initialized somehow); SomeRefObject ^ obj2(..initialized somehow); if (obj1 == obj2) // This compares the objects themselves for...
6
319
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 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...
3
273
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
1739
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 based on the position of the point in the vector rather than its x,y or any other criterion. Thus 2 free point (which are not in the vector are always...
16
4324
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 <string>
5
3629
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 wikipedia. I'm wondering what is the general rule in type conversions. Thanks, Peng
3
1429
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 promotion seem to take place on the first line, but not the second? Is there a way it can be enabled for the second?
0
7502
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7434
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7692
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7946
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7791
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6026
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3491
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1921
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.