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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:31:55+00:00 2026-06-12T13:31:55+00:00

Here simple code I am not getting the expected output. #include<stdio.h> int main() {

  • 0

Here simple code I am not getting the expected output.

#include<stdio.h>

int main()
{
   char buf[1024];
   while(1)
   {
      fgets(buf,strlen(buf),stdin);
      printf("%s",buf);
      printf("hello");
   }
}

In the above code I am want whatever the string I enter from keyboard to be printed out to me the same and then hello.
As I know fgets() is a blocking function till I input a string from keyboard and press ENTER it will block the program till that time. So when I run it I expect it as follows

$ ./a.out
I input some text here <ENTER>
I input some text here 
hello

But actually what I am getting output is infinite loop of “hello” printed on terminal. Why my fgets() not blocking the program? Any idea?

  • 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-12T13:31:57+00:00Added an answer on June 12, 2026 at 1:31 pm

    That’s because the strlen of your buffer is probably zero. You should be using sizeof instead, which gives you the size of the array (1024) rather than the length of the string it contains (indeterminate here).

    It probably is a zero sized string in your particular case (*buf == '\0') since the fgets call is not blocking at all. Actually, it could have a length of one as well since the standard states:

    The fgets function reads at most one less than the number of characters specified by n
    from the stream pointed to by stream into the array pointed to by s. No additional
    characters are read after a new-line character (which is retained) or after end-of-file. A
    null character is written immediately after the last character read into the array.

    In actual fact, as a local variable that hasn’t been initialised, buf could contain anything so you’re unwise to rely on it (if it doesn’t contain a null terminator at all, you may even find yourself dumping core as the strlen runs off the end).

    If you want a tried and true input function, see here. It has buffer overflow protection, prompting, removal of the newline at the end, and clearing of the line remainder in the event it was too long. I’ll duplicate the code below to make this answer more self-contained.

    #include <stdio.h>
    #include <string.h>
    
    #define OK       0
    #define NO_INPUT 1
    #define TOO_LONG 2
    static int getLine (char *prmpt, char *buff, size_t sz) {
        int ch, extra;
    
        // Get line with buffer overrun protection.
        if (prmpt != NULL) {
            printf ("%s", prmpt);
            fflush (stdout);
        }
        if (fgets (buff, sz, stdin) == NULL)
            return NO_INPUT;
    
        // If it was too long, there'll be no newline. In that case, we flush
        // to end of line so that excess doesn't affect the next call.
        if (buff[strlen(buff)-1] != '\n') {
            extra = 0;
            while (((ch = getchar()) != '\n') && (ch != EOF))
                extra = 1;
            return (extra == 1) ? TOO_LONG : OK;
        }
    
        // Otherwise remove newline and give string back to caller.
        buff[strlen(buff)-1] = '\0';
        return OK;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the code for a simple page I have that is not working: <head>
Here is some simple code: DIR* pd = opendir(xxxx); struct dirent *cur; while (cur
I'm definitely not getting something here: I'm creating a simple function to replicate a
here my simple code to add filename and their associated icon to virtualtreeview PFileInfoRec
Here is a simple code that shows what I think is a bug when
Here is the super simple code i have: HtmlDocument htmlDoc = new HtmlDocument(); htmlDoc.OptionWriteEmptyNodes
I've got a quite strange problem here. I'm calling some simple code via Ajax.Updater:
Here is a simple piece of code: import java.io.*; public class Read { public
All my code is here,quite simple,and I don't konw where it goes wrong. Person
my sample code is here include 'simple_html_dom.php'; function get_all_links($url){ global $host; $html = new

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.