Header Files

What Is write() in C?

write() is a POSIX system call that sends bytes from a buffer to a file descriptor. Learn its declaration, return value and a short C example.

On this page
  1. Short answer
  2. Where write() is declared
  3. The function signature
  4. A small example
  5. Check the return value
  6. How it differs from printf
  7. Is write() standard C?
  8. Where header files fit

Short answer

write() is a function that sends a block of bytes from memory to an open file or other destination, such as standard output. It is a POSIX function, not part of the C standard library. POSIX covers systems such as Linux and macOS.

Where write() is declared

On POSIX systems, the declaration is in the <unistd.h> header:

#include <unistd.h>

The header holds the function’s prototype, and the operating system and C library provide the code. Without the include, the compiler does not know the function’s parameter types. Depending on the compiler and its version, you may see a warning or an error about an implicit declaration.

The function signature

ssize_t write(int fd, const void *buf, size_t count);
  • fd is the file descriptor to write to. STDOUT_FILENO (1) is standard output, and STDERR_FILENO (2) is standard error. Both constants are also defined in <unistd.h>.
  • buf points to the bytes to send.
  • count is the number of bytes to try to send.

The return value is the number of bytes actually written, or -1 if an error occurred. In that case, errno holds the reason. ssize_t is a signed integer type, so it can hold both a byte count and -1.

A small example

#include <unistd.h>

int main(void) {
    const char message[] = "Hello from write()\n";
    ssize_t written = write(STDOUT_FILENO, message, sizeof(message) - 1);

    if (written == -1) {
        return 1;
    }
    return 0;
}

sizeof(message) - 1 leaves out the terminating null character, which is not part of the text to send. Build and run it on a POSIX system:

gcc write_demo.c -o write_demo
./write_demo

The output is:

Hello from write()

Check the return value

write() can write fewer bytes than requested. That can happen with pipes and sockets, for example. Code that must send every byte needs a loop that keeps calling write() until the whole block is out:

#include <errno.h>
#include <unistd.h>

ssize_t write_all(int fd, const char *buf, size_t len) {
    size_t sent = 0;
    while (sent < len) {
        ssize_t n = write(fd, buf + sent, len - sent);
        if (n == -1) {
            if (errno == EINTR) {
                continue;  /* a signal interrupted the call, so try again */
            }
            return -1;  /* a real error: check errno for the reason */
        }
        sent += (size_t)n;
    }
    return (ssize_t)sent;
}

How it differs from printf

printf() and std::cout usually hold output in a library buffer before handing it to the operating system. write() asks the operating system to send the bytes right away, with no library buffer in between. That makes it a lower-level call. It is also why mixing write() and printf() on the same output stream can produce text in an unexpected order.

Is write() standard C?

No. The C standard does not define write(). Its source is POSIX. On Windows, the Microsoft C runtime provides a similar function named _write, declared in <io.h>. Code meant to run on every platform usually uses portable calls such as fwrite() instead.

Where header files fit

write() shows the header file idea in action. <unistd.h> declares the function, and the code lives in a library that the linker connects to the program. A typical c header file example works the same way: the header says what exists, and a source file says how it works.

The complete guide to header files

C++ Header File Example
Read the guide

Keep exploring

Guides from other topics

All topics →

Rust vs C++ Performance

Rust and C++ both compile to fast native code, so speed gaps are often small. Learn what drives performance in each, and how to read benchmarks fairly.

Rust vs C++ 6 min read