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

Discussion in 'C Programming' started by Ben Fitzgerald, Jun 26, 2003.

  1. On Thu, 26 Jun 2003 18:55:22 GMT, dbtid <> wrote:
    > On 26 Jun 2003 18:25:52 GMT, Ben Fitzgerald <>
    > 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
    Ben Fitzgerald, Jun 26, 2003
    #1
    1. Advertising

  2. 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
    Hallvard B Furuseth, Jun 26, 2003
    #2
    1. Advertising

  3. Ben Fitzgerald

    dbtid Guest

    On 26 Jun 2003 18:25:52 GMT, Ben Fitzgerald <>
    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;
    dbtid, Jun 26, 2003
    #3
  4. On Thu, 26 Jun 2003 19:13:20 GMT, dbtid <> wrote:
    > Ben Fitzgerald wrote:
    >> On Thu, 26 Jun 2003 18:55:22 GMT, dbtid <> wrote:
    >>
    >>>On 26 Jun 2003 18:25:52 GMT, Ben Fitzgerald <>
    >>>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
    Ben Fitzgerald, Jun 26, 2003
    #4
  5. Ben Fitzgerald

    dbtid Guest

    Ben Fitzgerald wrote:
    > On Thu, 26 Jun 2003 18:55:22 GMT, dbtid <> wrote:
    >
    >>On 26 Jun 2003 18:25:52 GMT, Ben Fitzgerald <>
    >>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...
    dbtid, Jun 26, 2003
    #5
  6. Ben Fitzgerald

    dbtid Guest

    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.
    >
    dbtid, Jun 26, 2003
    #6
  7. Ben Fitzgerald

    Micah Cowan Guest

    dbtid <> 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
    Micah Cowan, Jun 27, 2003
    #7
  8. 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 :
    "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
    Richard Heathfield, Jun 27, 2003
    #8
  9. Ben Fitzgerald

    Dan Pop Guest

    In <> Richard Heathfield <> 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:
    Dan Pop, Jun 27, 2003
    #9
  10. On 27 Jun 2003 16:00:19 GMT, Dan Pop <> wrote:
    > In <> Richard Heathfield <> 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
    Ben Fitzgerald, Jun 27, 2003
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,199
    Martien Verbruggen
    Jun 27, 2003
  2. Ben Fitzgerald

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

    Ben Fitzgerald, Jun 27, 2003, in forum: C Programming
    Replies:
    1
    Views:
    6,523
    Micah Cowan
    Jun 28, 2003
  3. Chris Torek

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

    Chris Torek, Jun 27, 2003, in forum: C Programming
    Replies:
    1
    Views:
    1,289
    Dan Pop
    Jun 30, 2003
  4. trey

    newbie: char* int and char *int

    trey, Sep 10, 2003, in forum: C Programming
    Replies:
    7
    Views:
    397
    Irrwahn Grausewitz
    Sep 10, 2003
  5. Hal Styli
    Replies:
    14
    Views:
    1,615
    Old Wolf
    Jan 20, 2004
Loading...

Share This Page