bug-standards
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Some suggested corrections to the standards document


From: Eugene Y. Vasserman
Subject: Re: Some suggested corrections to the standards document
Date: Wed, 08 Nov 2006 23:30:54 -0600
User-agent: Thunderbird 1.5.0.8 (Windows/20061025)

The GNU document already states not to do the error check inline, so
this should really be:
int c;
c = getchar();
while (c != EOF)
{
   unsigned char u = c;
   write (file_descriptor, &u, 1);
   c = getchar();
}
This, of course, assumes that we are in fact reading a character buffer.
If we were reading an arbitrary stream, we should check for end-of-file
with the eof() call.

Thus spake Paul Eggert:
> address@hidden (Karl Berry) writes:
> 
>> True enough.  I have to shame-facedly confess that I'm not sure of the
>> recommended way to write it.  Introduce a char variable, like the below?
>> Use a cast somehow?  Paul?
> 
> What you had was the right idea, except it should use unsigned char
> instead of char, to be portable to (admittedly weird) systems where
> char is signed and there's integer overflow checking so an exception
> is raised when storing (say) 255 into a char variable.  So this is a
> bit better:
> 
>   int c;
>   while ((c = getchar ()) != EOF)
>     {
>       unsigned char u = c;
>       write (file_descriptor, &u, 1);
>     }

-- 
Eugene Y. Vasserman
http://www.cs.umn.edu/~eyv/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]