Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: getchar returns int, assign to array of char?

Reply
Thread Tools

Re: getchar returns int, assign to array of char?

 
 
Ben Fitzgerald
Guest
Posts: n/a
 
      06-26-2003
On Thu, 26 Jun 2003 18:55:22 GMT, dbtid <(E-Mail Removed)> wrote:
> On 26 Jun 2003 18:25:52 GMT, Ben Fitzgerald <(E-Mail Removed)>
> wrote:
>
>> now for the code:
>>
>> int c, i = 0;
>> char x[20];
>>
>> while((c = getchar()) != EOF)
>> x[i++] = c;
>>

>
> x[i++] = (char) c;


Okay, if that's the received comp.lang.c wisdom, I sign up!

Thanks,


--
Ben Fitzgerald
London, UK
 
Reply With Quote
 
 
 
 
Hallvard B Furuseth
Guest
Posts: n/a
 
      06-26-2003
dbtid wrote:

>> while((c = getchar()) != EOF)
>> x[i++] = c;

>
> x[i++] = (char) c;


No, that cast is pointless. c is converted to char anyway. x[i++] = c;
is fine.

Well, theoretically you have a problem if char is signed and overflow
doesn't silently subtract UCHAR_MAX+1, but I've never seen code which
worries about that.

--
Hallvard
 
Reply With Quote
 
 
 
 
dbtid
Guest
Posts: n/a
 
      06-26-2003
On 26 Jun 2003 18:25:52 GMT, Ben Fitzgerald <(E-Mail Removed)>
wrote:

> Hi
>
> I have a quick and probably easy question.
>
> Had a go on usenet but couldn't find this one (perhaps
> due to poor keyword selection!).
>


Good for you for checking elsewhere first.

> two statements, both I believe are true:
>
> getchar returns an int.
> strings are normally stored in an array of chars.
>


Yes to both.


> now for the code:
>
> int c, i = 0;
> char x[20];
>
> while((c = getchar()) != EOF)
> x[i++] = c;
>


x[i++] = (char) c;

 
Reply With Quote
 
Ben Fitzgerald
Guest
Posts: n/a
 
      06-26-2003
On Thu, 26 Jun 2003 19:13:20 GMT, dbtid <(E-Mail Removed)> wrote:
> Ben Fitzgerald wrote:
>> On Thu, 26 Jun 2003 18:55:22 GMT, dbtid <(E-Mail Removed)> wrote:
>>
>>>On 26 Jun 2003 18:25:52 GMT, Ben Fitzgerald <(E-Mail Removed)>
>>>wrote:

>> Okay, if that's the received comp.lang.c wisdom, I sign up!

>
> I wouldn't go *that* far with it...


Okay, putting words into your mouth!

Seems others say don't worry. I'll keep an eye on this
but it's clearly not a big issue. Thanks for helping
settle my mind!

--
Ben Fitzgerald
London, UK
 
Reply With Quote
 
dbtid
Guest
Posts: n/a
 
      06-26-2003
Ben Fitzgerald wrote:
> On Thu, 26 Jun 2003 18:55:22 GMT, dbtid <(E-Mail Removed)> wrote:
>
>>On 26 Jun 2003 18:25:52 GMT, Ben Fitzgerald <(E-Mail Removed)>
>>wrote:
>>
>>
>>>now for the code:
>>>
>>>int c, i = 0;
>>>char x[20];
>>>
>>>while((c = getchar()) != EOF)
>>> x[i++] = c;
>>>

>>
>>x[i++] = (char) c;

>
>
> Okay, if that's the received comp.lang.c wisdom, I sign up!
>


I wouldn't go *that* far with it...

 
Reply With Quote
 
dbtid
Guest
Posts: n/a
 
      06-26-2003
Hallvard B Furuseth wrote:
> dbtid wrote:
>
>
>>>while((c = getchar()) != EOF)
>>> x[i++] = c;

>>
>>x[i++] = (char) c;

>
>
> No, that cast is pointless. c is converted to char anyway. x[i++] = c;
> is fine.


The cast may be pointless, but it prevents lint from choking on it.
That seemed to be what he was after.

>
> Well, theoretically you have a problem if char is signed and overflow
> doesn't silently subtract UCHAR_MAX+1, but I've never seen code which
> worries about that.
>


 
Reply With Quote
 
Micah Cowan
Guest
Posts: n/a
 
      06-27-2003
dbtid <(E-Mail Removed)> writes:

> > now for the code:
> >
> > int c, i = 0;
> > char x[20];
> >
> > while((c = getchar()) != EOF)
> > x[i++] = c;
> >

>
> x[i++] = (char) c;


Completely unnecessary. I dislike adding casts merely for the sake of
shutting up lint or compiler warnings, provided the code is correct. YMMV.

-Micah
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      06-27-2003
dbtid wrote:
>
> Hallvard B Furuseth wrote:
> > dbtid wrote:
> >

<snip>
> >>x[i++] = (char) c;

> >
> >
> > No, that cast is pointless. c is converted to char anyway. x[i++] = c;
> > is fine.

>
> The cast may be pointless, but it prevents lint from choking on it.
> That seemed to be what he was after.


Well, yes, it does. The jury will be out forever about whether it's
better to add pointless casts to "shut up the compiler" and thus get a
clean compile on good code, or whether it's better to keep the /code/
clean at the expense of a slightly soiled compile.

I seem to have moved from one camp to the other over the last few years.

<snip>

--
Richard Heathfield : http://www.velocityreviews.com/forums/(E-Mail Removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton

 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      06-27-2003
In <(E-Mail Removed)> Richard Heathfield <(E-Mail Removed)> writes:

>dbtid wrote:
>>
>> Hallvard B Furuseth wrote:
>> > dbtid wrote:
>> >

><snip>
>> >>x[i++] = (char) c;
>> >
>> >
>> > No, that cast is pointless. c is converted to char anyway. x[i++] = c;
>> > is fine.

>>
>> The cast may be pointless, but it prevents lint from choking on it.
>> That seemed to be what he was after.

>
>Well, yes, it does. The jury will be out forever about whether it's
>better to add pointless casts to "shut up the compiler" and thus get a
>clean compile on good code, or whether it's better to keep the /code/
>clean at the expense of a slightly soiled compile.


Am I the only one who remembers that the OP already knows how to disable
the warning in question, therefore he doesn't have to opt for any
compromise?

IMHO, a good standard C compiler should make lint checks pointless.
Just make the compiler complain if a function is used without a
prototype declaration in scope and provide such declarations in the
headers where they belong (for applications that take more than one
source file).

lint was designed in the early days of C, and its main purpose was to make
consistency checks between the various source files an application
consisted of, *in the absence of function prototypes*.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
 
Reply With Quote
 
Ben Fitzgerald
Guest
Posts: n/a
 
      06-27-2003
On 27 Jun 2003 16:00:19 GMT, Dan Pop <(E-Mail Removed)> wrote:
> In <(E-Mail Removed)> Richard Heathfield <(E-Mail Removed)> writes:
>
> lint was designed in the early days of C, and its main purpose was to make
> consistency checks between the various source files an application
> consisted of, *in the absence of function prototypes*.


Well, thanks to all who've come forward with their suggestions in this
thread. It's given me a lot more to think about.

I am going to turn off/ignore this warning in lint. I don't believe
the explicit cast is a good thing (though, Dan, I will look at your
cast of a pointer).

Thanks again to everyone for really ripping this to pieces!

--
Ben Fitzgerald
London, UK
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
How to assign a returns value of a javascript function to a hiddenfield in a webpart Chander Software 0 12-20-2007 09:14 AM
createImage sometime returns null and sometime returns non-null. vizlab Java 3 10-17-2007 11:21 AM
Re: getchar returns int, assign to array of char? Chris Torek C Programming 1 06-30-2003 12:25 PM
Re: getchar returns int, assign to array of char? Ben Fitzgerald C Programming 1 06-28-2003 02:24 AM



Advertisments