About getc and fgetc. Please help.

This is a discussion on About getc and fgetc. Please help. within the C Programming forums, part of the General Programming Boards category; Here is what I found from K&R 2e Originally Posted by K&R 2e int fgetc(FILE *stream) fgetc returns the next ...

  1. #1
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212

    About getc and fgetc. Please help.

    Here is what I found from K&R 2e
    Quote Originally Posted by K&R 2e
    int fgetc(FILE *stream)
    fgetc returns the next character of stream as an unsigned char (converted to an int), or EOF if end of file or error occurs.

    int getc(FILE *stream)
    getc is equivalent to fgetc except that if it is a macro, it may evaluate stream more than once.
    I cannot understand what's the difference between these two functions. What does it mean by "if it is a macro, it may evaluate stream more than once"? Could somebody please be kind enough to give me some examples. Thank you very much.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    If you pass a "value" to a macro, perhaps like this:
    Code:
    getc(fp++);
    and the macro does something like this:
    Code:
    #define getc(f) \
      ((--((f)->level) >= 0) ? (unsigned char)(*(f)->curp++) : \
        _fgetc (f))
    then the original is parsed like this:
    Code:
      ((--((fp++)->level) >= 0) ? (unsigned char)(*(fp++)->curp++) : \
        _fgetc (fp++))
    which may be bad.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    Oh...I see. That means getc might be implemented as a macro, but fgetc is implemented as a function.

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    But if they are both implemented as a function then what is the point of having one or the other?

  5. #5
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by sand_man
    But if they are both implemented as a function then what is the point of having one or the other?
    The C standard leaves the possibility that getc will be implemented as a macro that may evaluate the stream more than once. Just because your implementation uses a function doesn't mean that anyone elses will.

    The reason there are two is likely because macro's have intrinsically less overhead than functions for tiny pieces of code.

    Thus, if your expression that you pass to getc has no side effects, it's advisable to use getc. If it does have side effects, you should definitely use fgetc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getc stream evaluation
    By terminator in forum C Programming
    Replies: 13
    Last Post: 04-21-2008, 11:42 AM
  2. help with fgetc
    By agentsmith in forum C Programming
    Replies: 3
    Last Post: 04-15-2008, 11:08 AM
  3. getc Seg Fault
    By Tyris in forum C Programming
    Replies: 3
    Last Post: 05-25-2005, 01:00 PM
  4. fgetc() and getc()
    By The Dog in forum C Programming
    Replies: 2
    Last Post: 07-24-2002, 06:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21