getc stream evaluation

This is a discussion on getc stream evaluation within the C Programming forums, part of the General Programming Boards category; What's the meaning of the following: Code: int getc(FILE *stream) getc is equivalent to fgetc except that if it is ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    31

    getc stream evaluation

    What's the meaning of the following:

    Code:
    int getc(FILE *stream)
    getc is equivalent to fgetc except that if it is a macro, it may evaluate stream more than once.
    What the meaning of it?
    if it is a macro -- means?
    evaluate stream "more" than once -- means?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    "if it is a macro" means... if it is a macro. Things in the library can be macros or functions, as the library authors see fit. If getc is implemented as a macro, it is then allowed to access the stream (stdin, or the file) more than once.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    Quote Originally Posted by tabstop View Post
    "if it is a macro" means... if it is a macro. Things in the library can be macros or functions, as the library authors see fit. If getc is implemented as a macro, it is then allowed to access the stream (stdin, or the file) more than once.
    I want to know the following:

    getc can access the stream more than once mean what?

    similar to getchar?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    No, it means just what it says - it can access the file stream twice (read, write, do whatever). For performance reasons, that means you should use fgetc because it will only access it once.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by terminator View Post
    I want to know the following:

    getc can access the stream more than once mean what?

    similar to getchar?
    getchar is, according to the standard, equivalent to getc(stdin).

    An example for getc: you could, naively, think that this:
    Code:
    getc(fopen("all_my_data.txt", "r"));
    would only try to open your file once, because it looks like a function call and normally that's what would happen. BUT, macro implementations of getc do not follow all the nice function parameter handling rules; they just do text search-and-replace. So, there may be multiple places where stream is used in the macro (it is explicitly allowed as you mentioned); hence there may be multiple attempts to open the data file.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    Quote Originally Posted by Elysia View Post
    No, it means just what it says - it can access the file stream twice (read, write, do whatever). For performance reasons, that means you should use fgetc because it will only access it once.
    what does it mean?
    "It can access the file stream twice"?

    a stream is a set of characters terminated by a newline character....

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    No it's not. A "stream" is a data structure filled with information about a file.
    You're simply passing that stream around to functions that does stuff with that information (ie read or write).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    Quote Originally Posted by Elysia View Post
    No it's not. A "stream" is a data structure filled with information about a file.
    You're simply passing that stream around to functions that does stuff with that information (ie read or write).
    I am getting "round about" answers...
    can i please get answer uptothepoint?

    -stream means what?
    -evaluate stream more than once mean what?


    also, whats the use of
    Code:
     ungetc(int c, FILE *stream) 
    does it "push back" the character gotten by getchar()? so that the next call getchar() will give back the pushed back character?

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    No, look again at what tabstop posted. Using getc can, in effect, actually call fopen twice instead of one, which is what accessing the stream more than once means.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    31

    Thumbs down

    Quote Originally Posted by tabstop View Post
    getchar is, according to the standard, equivalent to getc(stdin).

    An example for getc: you could, naively, think that this:
    Code:
    getc(fopen("all_my_data.txt", "r"));
    would only try to open your file once, because it looks like a function call and normally that's what would happen. BUT, macro implementations of getc do not follow all the nice function parameter handling rules; they just do text search-and-replace. So, there may be multiple places where stream is used in the macro (it is explicitly allowed as you mentioned); hence there may be multiple attempts to open the data file.
    Whatever tapstop said above is going "bouncer" in my head...
    can anyone give me a "simpler" explanation?

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    In simpler words, if you use getc, you may attempt to open the same file twice.
    Instead of:
    Code:
    getc("all_my_data.txt");
    You get:
    Code:
    getc(fopen("all_my_data.txt", "r"));
    Or along the lines of.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by terminator View Post
    I am getting "round about" answers...
    can i please get answer uptothepoint?

    -stream means what?
    -evaluate stream more than once mean what?


    also, whats the use of
    Code:
     ungetc(int c, FILE *stream) 
    does it "push back" the character gotten by getchar()? so that the next call getchar() will give back the pushed back character?
    If you read what you yourself posted, stream is a expression of type FILE * (pointer to a file). That expression, in a macro, may be evaluated more than once. I'm hoping you know what more than once means, but let's look at an example that doesn't deal with FILE to make sure: suppose we do this
    Code:
    #define max(a,b) (a)>(b) ? (a) : (b)
    If a is the larger expression (that is, if (a)>(b) evaluates to true), then a will be evaluated twice -- once in the > comparison, and once as the true part of the ternary operator ?:. So if a was really count++, then count will get incremented twice instead of once.

    So in the file example, if stream is an expression/function call (like fopen or something even more outre involving fseek and a comma) that function may be called more than once.

    As to ungetc: why don't you keep reading? The bit right after the bit you posted says exactly what it does: it pushes the character c back onto the input stream, so that the next getc will read that character again. It also says that it is only guaranteed to work once in a row (so two successive ungetc's may fail).

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    getc(fopen("all_my_data.txt", "r"));
    I'm not sure why anyone would want to do that? How would you close it since you don't have a FILE handle now?

    But anyways, to answer the question for the millionth time, if you learn how macros work, including all the subtle pitfalls of using them, then you should understand what "evaluated more than once" means.

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    The "evaluate more than once" means things like this.

    Code:
    ch = fgetc( streamArray[streamnum++] );
    Will always do exactly what you expect it to.

    Code:
    ch = getc( streamArray[streamnum++] );
    On the other hand may result in the side effects happening more than once.

    If the stream expression is constant, then the macro form is safe to use.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Discard wrong data from stream
    By mesmer in forum C Programming
    Replies: 7
    Last Post: 11-16-2008, 01:30 AM
  2. Closing a stream
    By cunnus88 in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2008, 04:15 PM
  3. Help! About text stream and binary stream
    By Antigloss in forum C Programming
    Replies: 1
    Last Post: 09-01-2005, 08:40 AM
  4. fgetc() and getc()
    By The Dog in forum C Programming
    Replies: 2
    Last Post: 07-24-2002, 05:00 AM
  5. Performance Evaluation
    By rick barclay in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 09-16-2001, 10:16 AM

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