The C programming language 1.5.1 File Copying

Discussion in 'C Programming' started by Joseph Santoyo, Aug 13, 2011.

  1. The part where he says:

    "We can't use char since c must be big enough to hold EOF in addition
    to any possible char. Therefore we use int.

    Can someone explain this to me?
     
    Joseph Santoyo, Aug 13, 2011
    #1
    1. Advertising

  2. Joseph Santoyo

    Seebs Guest

    On 2011-08-13, Joseph Santoyo <> wrote:
    > The part where he says:


    > "We can't use char since c must be big enough to hold EOF in addition
    > to any possible char. Therefore we use int.


    > Can someone explain this to me?


    What about it?

    Okay, imagine that you want to have a function which gives you "the next
    character from the file". So we might initially write:

    char getchar();
    and it returns the next character.

    How should it indicate to you that there isn't a next character? Every
    value this function could *possibly* return is a character that could occur
    in a file.

    So instead we write
    int getchar();

    Now, if there's a character, it returns a character, and if there's not
    a character, it returns EOF, which is a value that isn't in the range
    of unsigned char, so you can be sure it doesn't represent a character.

    -s
    --
    Copyright 2011, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
     
    Seebs, Aug 13, 2011
    #2
    1. Advertising

  3. On Aug 12, 8:44 pm, Seebs <> wrote:
    > On 2011-08-13, Joseph Santoyo <> wrote:
    >
    > > The part where he says:
    > > "We can't use char since c must be big enough to hold EOF in addition
    > > to any possible char. Therefore we use int.
    > > Can someone explain this to me?

    >
    > What about it?
    >
    > Okay, imagine that you want to have a function which gives you "the next
    > character from the file".  So we might initially write:
    >
    >         char getchar();
    > and it returns the next character.
    >
    > How should it indicate to you that there isn't a next character?  Every
    > value this function could *possibly* return is a character that could occur
    > in a file.
    >
    > So instead we write
    >         int getchar();
    >
    > Now, if there's a character, it returns a character, and if there's not
    > a character, it returns EOF, which is a value that isn't in the range
    > of unsigned char, so you can be sure it doesn't represent a character.
    >
    > -s
    > --
    > Copyright 2011, all wrongs reversed.  Peter Seebach / ://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    > I am not speaking for my employer, although they do rent some of my opinions.


    Thanks, I understand it a lot better now.
     
    Joseph Santoyo, Aug 13, 2011
    #3
  4. Joseph Santoyo

    Joe Pfeiffer Guest

    Joseph Santoyo <> writes:

    > The part where he says:
    >
    > "We can't use char since c must be big enough to hold EOF in addition
    > to any possible char. Therefore we use int.
    >
    > Can someone explain this to me?


    You pick a char value to mean EOF (let's assume an eight-bit char here,
    so you've got 256 to choose from), and I'll give you a file containing
    that char as a byte in the file, which your program will misinterpret as
    being the end of the file.
     
    Joe Pfeiffer, Aug 13, 2011
    #4
    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. Replies:
    0
    Views:
    508
  2. chris kramer
    Replies:
    3
    Views:
    419
    Victor Bazarov
    Jan 25, 2004
  3. Replies:
    2
    Views:
    444
    Larry Bates
    Jan 11, 2006
  4. Casey Hawthorne
    Replies:
    4
    Views:
    1,059
    Jarek Zgoda
    Aug 4, 2006
  5. Jared Wiltshire
    Replies:
    8
    Views:
    432
    Ian Collins
    Aug 7, 2006
Loading...

Share This Page