getc and fgetc confusion

This is a discussion on getc and fgetc confusion within the C Programming forums, part of the General Programming Boards category; Hi all, a small confusion is there for me regarding the getc and fgetc functions. i have written this small ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    89

    getc and fgetc confusion

    Hi all,

    a small confusion is there for me regarding the getc and fgetc functions. i have written this small program
    Code:
    int main(int argc, char *argv[])
    {
        int c;
        while((c = fgetc(stdin)) != EOF)
        {
            fputc(c,stdout);
        }
    
    return 0;
    }
    Let us say i have input "qwmb" followed with [enter]. Then the output of the program is "qwmb" and the control would go to the new line. now my doubt is once you entered the input as q then what is the value the variable c is holding and if i enter w is the variable c not over written? if i press [enter] how does the variable c hold the complete value "qwmb"?
    could somebody please explain me this concept?

    thanks in advance,
    regards,
    satya

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    The variable c doesn't hold anything but one character at a time. The reason why your program does this is because of input buffering: fgetc() just returns the next character in the stream. When you enter "qwmb" and press return, each subsequent call to fgetc() will get the next character ('q', 'w', ...), and each subsequent call to fputc() will then output this character.

    So, to answer your question, the variable c is actually used to hold all the letters, but one at a time.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Your c variable is overwritten in each iteration, but stdin is line buffered. Which means what you type is entered into a buffer, and when you hit enter, fgetc() reads from that buffer one character at a time.

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    89
    thank you all for the replies. Now it opened up lot of questions for me why line buffering is required and etc, etc, i will try to google and try to get answers.

    thanks once again,
    regards,
    satya

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with my getc
    By tombocollbo in forum C Programming
    Replies: 1
    Last Post: 12-09-2010, 12:24 PM
  2. getc
    By taurus in forum C Programming
    Replies: 5
    Last Post: 11-13-2008, 06:55 AM
  3. About getc and fgetc. Please help.
    By Antigloss in forum C Programming
    Replies: 4
    Last Post: 09-28-2005, 04:00 AM
  4. fgetc() and getc()
    By The Dog in forum C Programming
    Replies: 2
    Last Post: 07-24-2002, 05:00 AM
  5. getc or not getc that is the problem
    By Juan Roberto in forum C Programming
    Replies: 3
    Last Post: 11-13-2001, 11:43 AM

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