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 8564729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:15:50+00:00 2026-06-11T17:15:50+00:00

Executing the following C code #include <stdio.h> int main(int argc, char **argv) { char

  • 0

Executing the following C code

#include <stdio.h>
int main(int argc, char **argv) {
  char stdinput[10];
  while (1) {
    fgets(stdinput, 10, stdin);
    fputs(stdinput, stdout);
  }
}

produces:

By console:

./a.out
input
input

and then it waits for more input. That is, it echoes stdin to stdout, similarly to cat.

By pipe:

echo input | ./a.out
input
input
input
[...]

after being started, it floods the console, all by itself, without interaction.

This example program is exactly what I used for the tests; it’s not a cut.
I would expect the two tests to behave the same way. What’s happening?

  • 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-06-11T17:15:52+00:00Added an answer on June 11, 2026 at 5:15 pm

    Once EOF is reached, fgets returns NULL immediately without waiting for input (or modifying the buffer). Thus, it will loop infinitely. In your pipe case, echo will close the pipe once it has written "input\n", resulting in EOF.

    Change your fgets call to

    if(fgets(stdinput, 10, stdin) == NULL)
        break;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following code: #include <stdio.h> int main() { int *p; p = (int
#include <QtCore/QCoreApplication> #include <QtGui/QLabel> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QLabel
I get an off by one error while executing the following code #include <iostream>
I am executing the following code: def takeN(s : String, n : Int): String
I get an exception while executing following piece of code bool FieldValueMessage::Get(const std::string &field,
I am using the following code for a fork execution #include <stdio.h> #include <sys/types.h>
I'm trying to build the following sample code: #include cv.h #include highgui.h #include <stdio.h>
#include <stdio.h> #include <stdint.h> #include <stdlib.h> int main() { FILE* bmp = NULL; uint32_t
I have the following code in cuda_computation.cu #include <iostream> #include <stdio.h> #include <cuda.h> #include
When executing the following code: OpenFileDialog dialog = new OpenFileDialog(); DialogResult result = dialog.ShowDialog();

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.