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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:20:51+00:00 2026-05-18T04:20:51+00:00

Possible Duplicate: Why does printf not flush after the call unless a newline is

  • 0

Possible Duplicate:
Why does printf not flush after the call unless a newline is in the format string? (in C)

I’m using the sleep() function in C, and am running into a problem: I wasn’t sure that this was the problem so I boiled the entire code down to this:

int main() {

  printf("1");
  sleep(3);
  printf("2");

  return 0;
}

What I thought this should produce is 1 .. wait for 3 seconds .. 2. Instead the program waits for 3 seconds and then prints 12. Is there any way to use the sleep function so that I get the first output?

Thanks

  • 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-18T04:20:52+00:00Added an answer on May 18, 2026 at 4:20 am

    It’s not actually the sleep function which is delaying the output, it’s the buffering nature of the standard output stream. The output of 2 is almost certainly also delayed until your program exits main but the delay there is so small you’re not noticing it.

    Standard output is line buffered if it can be detected to refer to an interactive device (otherwise it’s fully buffered).

    If you fflush (stdout) after every output call that you want to see immediately, that will solve the problem.

    Alternatively, you can use setvbuf before operating on stdout, to set it to unbuffered and you won’t have to worry about adding all those fflush lines to your code:

    setvbuf (stdout, NULL, _IONBF, BUFSIZ);
    

    Just keep in mind that may affect performance quite a bit if you’re sending the output to a file. Also keep in mind that support for this is implementation-defined, not guaranteed by the standard.

    ISO C99 section 7.19.3/3 is the relevant bit:

    When a stream is unbuffered, characters are intended to appear from the source or at the destination as soon as possible. Otherwise characters may be accumulated and transmitted to or from the host environment as a block.

    When a stream is fully buffered, characters are intended to be transmitted to or from the host environment as a block when a buffer is filled.

    When a stream is line buffered, characters are intended to be transmitted to or from the host environment as a block when a new-line character is encountered.

    Furthermore, characters are intended to be transmitted as a block to the host environment when a buffer is filled, when input is requested on an unbuffered stream, or when input is requested on a line buffered stream that requires the transmission of characters from the host environment.

    Support for these characteristics is implementation-defined, and may be affected via the setbuf and setvbuf functions.

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

Sidebar

Related Questions

Possible Duplicate: Why does printf not flush after the call unless a newline is
Possible Duplicate: Why does Mac's $find not have the option -printf? Not sure what
Possible Duplicate: Read/Write Python Closures In the following function, the internal function does not
Possible Duplicate: warning: format not a string literal and no format arguments I have
Possible Duplicate: What does “(void) new” mean in C++? I'm not familiar with C++
Possible Duplicate: What does :_* (colon underscore star) do in Scala? I’m using the
Possible Duplicate: Invalid read/write sometimes creates segmentation fault and sometimes does not I have
Possible Duplicate: How does dereferencing of a function pointer happen? Hi All, Why these
Possible Duplicate: Why does printf print wrong values? #include<stdio.h> int main(int argc, const char
Possible Duplicate: Simple C scanf does not work? Why does scanf(%c, &letter); not working.

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.