473,800 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

if EOF = -1, can't a valid character == EOF and cause problems?

My question is about EOF (which is (int)-1 on the
implementation I use). Type char on my implementation
is 'signed' too.

How is it that the input library functions that return
EOF can be used properly if something like ASCII 255
is read from the input file? 255 would be returned as -1,
and would be compared against EOF and cause problems.

What's the point of declaring a int to hold the return
value from these functions, when a char(on my implementation)
can represent a -1(EOF on my representation) also?

Example;

char c_char;
while( (c_char = getchar()) != EOF )
{ /* something */ }

will work just as *badly* as:

int c_int;
while( (c_int= getchar()) != EOF )
{ /* something */ }
I'm not complaining about the AMBIGUITY of
(int)EOF vs.(int)(Ascii 255 returned as -1)
but wondering why people suggent capturing the
return value from functions like getchar into
an int, when the int doesn't help us get rid
of this ambiguous case?

I've tried to research this to death and wrap my
mind around a possible answer, but I have failed
miserably. I need someone to help me turn on the
flickering lightbulb.

Much thanks in advance

Nov 14 '05 #1
10 4105
Kobu <ko********@gma il.com> scribbled the following:
My question is about EOF (which is (int)-1 on the
implementation I use). Type char on my implementation
is 'signed' too. How is it that the input library functions that return
EOF can be used properly if something like ASCII 255
is read from the input file? 255 would be returned as -1,
and would be compared against EOF and cause problems. What's the point of declaring a int to hold the return
value from these functions, when a char(on my implementation)
can represent a -1(EOF on my representation) also?
You just answered your own question. The functions return int
precisely because of the need to distinguish between -1 and 255.
Example; char c_char;
while( (c_char = getchar()) != EOF )
{ /* something */ } will work just as *badly* as: int c_int;
while( (c_int= getchar()) != EOF )
{ /* something */ }
No, it won't. The second version will work much better. You see,
although as a signed char, 255 is the same as -1, as a signed int,
255 is 255 and -1 is -1, and never the twain shall meet.
I'm not complaining about the AMBIGUITY of
(int)EOF vs.(int)(Ascii 255 returned as -1)
but wondering why people suggent capturing the
return value from functions like getchar into
an int, when the int doesn't help us get rid
of this ambiguous case?
But it does help.
I've tried to research this to death and wrap my
mind around a possible answer, but I have failed
miserably. I need someone to help me turn on the
flickering lightbulb. Much thanks in advance


I hope I have been of help.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"How can we possibly use sex to get what we want? Sex IS what we want."
- Dr. Frasier Crane
Nov 14 '05 #2
On 3 Mar 2005 13:10:56 -0800, "Kobu" <ko********@gma il.com> wrote:
My question is about EOF (which is (int)-1 on the
implementati on I use). Type char on my implementation
is 'signed' too.

How is it that the input library functions that return
EOF can be used properly if something like ASCII 255
is read from the input file? 255 would be returned as -1,
and would be compared against EOF and cause problems.

What's the point of declaring a int to hold the return
value from these functions, when a char(on my implementation)
can represent a -1(EOF on my representation) also?


In the case where integers and characters are not the same
length, you'll find that char 255 appears as an integer 255
and -1 appears as -1.

This is a widely used interview question, so read up on it.

Also learn how to swap bytes without using a temporary, how
to implement atoi() and how to reverse a list and you're mostly there!

Now, there are 5 pirates ....

Nov 14 '05 #3
"Kobu" <ko********@gma il.com> writes:
My question is about EOF (which is (int)-1 on the
implementation I use). Type char on my implementation
is 'signed' too.

How is it that the input library functions that return
EOF can be used properly if something like ASCII 255
is read from the input file? 255 would be returned as -1,
and would be compared against EOF and cause problems.

What's the point of declaring a int to hold the return
value from these functions, when a char(on my implementation)
can represent a -1(EOF on my representation) also?

Example;

char c_char;
while( (c_char = getchar()) != EOF )
{ /* something */ }

will work just as *badly* as:

int c_int;
while( (c_int= getchar()) != EOF )
{ /* something */ }


getchar() returns the next character from stdin *as an unsigned char*
converted to int. If the character read has the value 255, getchar()
will return 255, even if plain char is signed. At end-of-file,
getchar() returns the distinct value EOF (-1 on your system).

If you assign the result of getchar() to a variable of type
char, you lose the ability to distinguish between 255 and -1.

(There's still a potential problem on exotic systems where char and
int are the same size, but you almost certainly don't need to worry
about that.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4
Actually I think you've confused the Control Flags value -1 for the Data
being read. You can actually read both at the same time. ( 0x0ff, and EOF
flag)

"Kobu" <ko********@gma il.com> wrote in message
news:11******** **************@ l41g2000cwc.goo glegroups.com.. .
My question is about EOF (which is (int)-1 on the
implementation I use). Type char on my implementation
is 'signed' too.

How is it that the input library functions that return
EOF can be used properly if something like ASCII 255
is read from the input file? 255 would be returned as -1,
and would be compared against EOF and cause problems.

What's the point of declaring a int to hold the return
value from these functions, when a char(on my implementation)
can represent a -1(EOF on my representation) also?

Example;

char c_char;
while( (c_char = getchar()) != EOF )
{ /* something */ }

will work just as *badly* as:

int c_int;
while( (c_int= getchar()) != EOF )
{ /* something */ }
I'm not complaining about the AMBIGUITY of
(int)EOF vs.(int)(Ascii 255 returned as -1)
but wondering why people suggent capturing the
return value from functions like getchar into
an int, when the int doesn't help us get rid
of this ambiguous case?

I've tried to research this to death and wrap my
mind around a possible answer, but I have failed
miserably. I need someone to help me turn on the
flickering lightbulb.

Much thanks in advance

Nov 14 '05 #5
DHOLLINGSWORTH2 wrote:
Actually I think you've confused the Control Flags value -1 for the Data being read. You can actually read both at the same time. ( 0x0ff, and EOF flag)


Please don't top-post, and please don't answer questions
if you have no idea what you are talking about.

Nov 14 '05 #6
Keith Thompson wrote:
(There's still a potential problem on exotic systems where char and
int are the same size, but you almost certainly don't need to worry
about that.)


Use feof,
to distinguish cases when getchar is supposed to return EOF
from cases where there is an end of file condition.

--
pete
Nov 14 '05 #7


pete wrote:
Keith Thompson wrote:

(There's still a potential problem on exotic systems where char and
int are the same size, but you almost certainly don't need to worry
about that.)

Use feof,
to distinguish cases when getchar is supposed to return EOF
from cases where there is an end of file condition.


In C, EOF is only indicated after an input routine has tried to read,
and has reached end-of-file. So the input function (getchar in this
case) must first reach the end of the file and return EOF and the
end-of-file indicator for the stream is set. Calling function
feof before the input function has reached the end of the file will
not indicate that the next getchar call will return EOF.
Read faq question 12.2:
http://www.eskimo.com/~scs/C-faq/q12.2.html
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapi dsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #8
In <42**********@m indspring.com> pete <pf*****@mindsp ring.com> writes:
Keith Thompson wrote:
(There's still a potential problem on exotic systems where char and
int are the same size, but you almost certainly don't need to worry
about that.)


Use feof,
to distinguish cases when getchar is supposed to return EOF
from cases where there is an end of file condition.


1. If there is an end of file condition, getchar() is supposed to
return EOF, so there is no distinction.

2. If you don't want to rely on EOF, you have to use both ferror() and
feof().

Dan
--
Dan Pop <Da*****@ifh.de >
Nov 14 '05 #9
John <jo*******@hotm ail.com> wrote:
Also learn how to swap bytes without using a temporary


If you figure out how to do it in standard C, I'd be curious to
know.

--
With sufficient thrust, pigs fly just fine. However, this is
not necessarily a good idea. It is hard to be sure where they
are going to land, and it could be dangerous sitting under them
as they fly overhead. -- RFC 1925
Nov 14 '05 #10

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

Similar topics

13
2009
by: Sheperd | last post by:
Hey everyone, I have this browser detect script and the validator is having problems with it, im trying to validate a page for XHTML 1.0 Transitional If you have any ideas or suggestions they would be greatly appericated Here is the script and the error that validtor spits out
3
1720
by: John | last post by:
I am new to c++ programming and i just want to make a program that takes a users name, then converts it to a hex string, then puts it together in a fixed order. An example output would be: Name: John Output: 84721305 this is my generation code so far #include <conio.h>
7
5731
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning is to improve, but not to prove.
6
4904
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of the html page controls the form fields that are required. It doesn't function like it's supposed to and I can leave all the fields blank and it still submits the form. Also I can't get it to transfer the file in the upload section. The file name...
26
3065
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I read some of the items from the bottom up of the buffer, and some from the top down, moving the bottom items back to the new re-allocated bottom on every file read. Then when I've read all four files, I sort the top and bottom items separately...
3
2184
by: Jamie Risk | last post by:
I'm attempting to improve some serially executing code (that uses the SerialPort class) bogging Windows down when it runs. To do the 'antibogging' I'm following the example from MSDN Windows.IO.Ports.SerialPort page and use threading. I'm not sure if I'm creating problems with this implementation and would appreciate your input. The original serial code: {
8
1674
by: weidongtom | last post by:
Hi, I was reading some code and I came across this function: static char * base_name(char *s) {
14
1497
by: jois.de.vivre | last post by:
Hello, I was wondering if the following code was ok: // ----------- start code #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char** argv) {
4
4590
by: zion4ever | last post by:
Hello good people, Please bear with me as this is my first post and I am relative new to ASP. I do have VB6 experience. I have a form which enables users within our company to do an intranet reservation of available resources (laptops, beamers, etc). The MySql database queries are already in place, as is the ASP administration panel. The frontend that users will see however, still needs some work. I'm really close, but since I'm no...
0
9551
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10507
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9092
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7582
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5473
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5607
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4150
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
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2948
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.