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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:54:26+00:00 2026-05-30T13:54:26+00:00

So I know printf() is higher level than write() and ends up using write()

  • 0

So I know printf() is higher level than write() and ends up using write(). Printf() is buffered and write() makes system calls.

Example 1, if I were to run a program with printf() before write() then it would output the value of printf() before the value of write().

Example 2, if I were to run the same program and have it go through output redirection into a file, the value of write() outputs before printf().

#include <stdio.h>
#include <unistd.h>

int main()
{
    printf("This is a printf test\n");
    write(STDOUT_FILENO, "This is a write test\n", 21);
    return 0;
}

I don’t understand what is happening here. In example 1, is the program waiting for printf()s output before running write()? In example 2, is the program redirecting the first output that is ready? And because write() is lower level, and does not need to buffer like printf() then it is printed first?

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

    You answered your own question.

    printf is buffered and write is not.

    For output to a terminal, the C stdio system has a feature that it flushes the buffers whenever it sees a newline '\n'. For more about stdio buffering look at the documentation for setvbuf.

    But when output is to a file to increase speed the stdio system does not flush the buffer. That is why write output appears first.

    Here is some of the strace output from running on my Linux system:

    fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 1), …}) = 0
    mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7880b41000
    write(1, “This is a printf test\n”, 22) = 22
    write(1, “This is a write test\n\0”, 22) = 22

    The fstat is where the stdio system detects the type of output file descriptor 1 is connected to. I believe it looks at st_mode and sees that it is a character device. A disk file would be a block device. The mmap is the memory allocation for the stdio buffer, which is 4K. Then the output is written.

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

Sidebar

Related Questions

printf(/*something else*/); /*note that:without using \n in printf*/ I know printf() uses a buffer
I know how to use printf() to format output of float, for example: float
For example, printf is dynamically linked. But how does the compiler(gcc) know that?
How can I print a non-null-terminated string using printf, assuming that I know the
I know you can print with printf() and puts() . I can also see
Does anyone know a 100% clone of the C/C++ printf for Delphi? Yes, I
How does printf handle its arguments? I know that in C# I can use
Does anyone know a good safe way to redirect the output of a printf-style
I want to know if the below code: <?php printf (%s, $some_variable); ?> is
I know the printf statement in PHP can format strings as follows: //my variable

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.