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

  • Home
  • SEARCH
  • 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 6947181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:39:40+00:00 2026-05-27T13:39:40+00:00

This is my very first post on Stack Overflow, so I hope I don’t

  • 0

This is my very first post on Stack Overflow, so I hope I don’t step on anyone’s toes.

Of course, all inputs are welcome and appreciated, but those most suited to answer would have actually read the book, C Programming Language, 2nd ed.

I have just finished coding Exercise 6-4, but I cannot seem to figure something out. Why does the getword() function not read EOF until I press Ctrl+D (I code in C in an Arch Linux VM)?

Many of my previous exercises from the book require reading from stdin. One way I would do it is via something like

while ((c = getchar()) != EOF) {...}

In such an instance, I never have to press Ctrl+D. I enter in my input, press Enter, the stdin buffer gets flushed out, and EOF is detected automatically. The getword() function also relies on getchar() at its base, so why does it hang my program?

The getword() function is called from main():

while (getword(word, MAX_WORD) != EOF) {
    if (isalpha(word[0])) {
        root = addtree(root, word);
    }
}

The getword() function itself:

int getword(char *word, int lim) {

    char *w = word;
    int c;

    while (isspace(c = getch())) {
    }
    if (c != EOF) {
        *w++ = c;
    }
    // This point is reached
    if (!isalpha(c)) {
        // This point is never reached before Ctrl+D
        *w = '\0';
        return c;
    }
    for ( ; --lim > 0; w++) {
        if (!isalnum(*w = getch())) {
            ungetch(*w);
            break;
        }
    }
    *w = '\0';
    return word[0];
}

I put comments to indicate the point where I determined that EOF is not being read.

The getch() and ungetch() functions are the same ones used in the Polish notation calculator from Chapter 4 (and that program was able to read EOF automatically – by pressing Enter):

#define BUF_SIZE 100

char buf[BUF_SIZE];
int bufp = 0;

int getch(void) {

    return (bufp > 0) ? buf[--bufp] : getchar();
}

void ungetch(int c) {

    if (bufp >= BUF_SIZE) {
        printf("ungetch: too many characters\n");
    }
    else {
        buf[bufp++] = c;
    }
}

Thus far, this is the first program I wrote since the beginning of this book that requires me to manually enter the EOF via Ctrl+D. I just can’t seem to figure out why.

Much appreciation in advance for explanations…

  • 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-27T13:39:40+00:00Added an answer on May 27, 2026 at 1:39 pm

    Having to type Ctrl+D to get EOF is the normal behavior for Unix-like systems.

    For your code snippet:

    while ((c = getchar()) != EOF) {...}
    

    pressing Enter definitely shouldn’t terminate the loop (unless your tty settings are badly messed up).

    Try compiling and running this program:

    #include <stdio.h>
    int main( void )
    {
        int c;
        while ((c = getchar()) != EOF) {
            putchar(c);
        }
        return 0;
    }
    

    It should print everything you type, and it should terminate only when you type control-D at the beginning of a line (or when you kill it with control-C).

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

Sidebar

Related Questions

This is my very first question in stack overflow and I am quite excited.
this is my first post here on stackoverflow and am very impressed by the
This is very basic magento question i guess. I want to first get all
First of all thanks in advance, this has been very frustrating and I'm hoping
First of I'm very sorry but this questions is not so so specific. All
My first post on this site with huge hope:: I am trying to understand
This is my very first post on StackOverflow. Basically, my app starts by checking
this is my very first post! I have a quick question in regarding inheritance
This is my first stack overflow quesiton, so if i'm not posting correctly, or
Background First of all, much gratitude to atebits for their very informative blog post

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.