Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6672757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:32:45+00:00 2026-05-26T03:32:45+00:00

A related question is here , but my question is different. But, I’d like

  • 0

A related question is here, but my question is different.

But, I’d like to know more about the internals of getchar() and stdin. I know that getchar() just ultimately calls fgetc(stdin).

My question is about buffering, stdin and getchar() behavior. Given the classic K&R example:

#include <stdio.h>

main()
{
    int c;

    c = getchar();
    while (c != EOF) {
        putchar(c);
        c = getchar();
    }
}

It seems to me that getchar()’s behavior could be described as follows:

If there’s nothing in the stdin buffer, let the OS accept user input until [enter] is pressed. Then return the first character in the buffer.

Assume the program is run and the user types “anchovies.”

So, in the above code listing, the first call to getchar() awaits user input and assigns the first character in the buffer to variable c. Inside the loop, the first iteration’s call to getchar() says “Hey, there’s stuff in the buffer, return the next character in the buffer.” But the Nth iteration of the while loop results in getchar() saying “Hey, there’s nothing in the buffer, so let stdin gather what the user types.

I’ve spend a little time with the c source, but it seems this is more of a behavioral artifact of stdin rather than fgetc().

Am I wrong here? Thanks for your insight.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:32:46+00:00Added an answer on May 26, 2026 at 3:32 am

    I know that getchar() just ultimately calls fgetc(stdin).

    Not necessarily. getchar and getc might as well expand to the actual procedure of reading from a file, with fgetc implemented as

    int fgetc(FILE *fp)
    {
        return getc(fp);
    }
    

    Hey, there’s nothing in the buffer, so let stdin gather what the user types.
    […] it seems this is more of a behavioral artifact of stdin rather than fgetc().

    I can only tell you what I know, and that is how Unix/Linux works. On that platform, a FILE (including the thing that stdin points to) holds a file descriptor (an int) that is passed to the OS to indicate from which input source the FILE gets data, plus a buffer and some other bookkeeping stuff.

    The “gather” part then means “call the read system call on the file descriptor to fill the buffer again”. This varies per implementation of C, though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(This question is related to another one , but different enough that I think
Here are two different questions but I think they are related. When using Git,
Related to my question here but not enough to open a new question. I
Related question [stackoverflow] here . I'm trying to do the above, but I want
The question is closely related to my past question here, but it is not
Here is a related question I found, but it does not answer my question
not sure if this question should be here or in serverfault, but it's java-related
This is somewhat related to my previous question but I've realised that I needed
This is related to a previous question , but it seems different enough to
I have seen the related question here: An object with the same key already

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.