printf library function vs write system call
#include <unistd.h>
int main() {
write(1, "Hello, World\n", 13);
return 0;
}
#include <stdio.h>
int main() {
printf("Hello, World\n");
return 0;
}
which one is better? what's the difference between the two?
The two code snippets you’ve presented both print "Hello, World" to standard output, but they differ significantly in abstraction level, portability, and typical use case.
1. Low-Level Version (with write)
#include <unistd.h>
int main() {
write(1, "Hello, World\n", 13);
return 0;
}
🔧 Characteristics:
-
System Call Interface:
write()is a direct system call interface provided by the OS (POSIX).1is the file descriptor for standard output. -
Low-level / Unix-specific: It bypasses the standard C library and uses raw system-level functionality. This makes it non-portable outside POSIX systems (Linux, FreeBSD, etc.).
-
No buffering: Writes immediately to the output without buffering. Suitable when you want guaranteed immediate output (e.g. in system-level programs, daemons, or logging mechanisms).
-
Manual memory management: You must manually specify the string length.
✔ Pros:
-
Minimal and fast.
-
Predictable timing—no buffering.
-
Good for system programming or educational purposes.
✘ Cons:
-
Not portable to non-POSIX systems (e.g., Windows without Cygwin).
-
Less readable.
-
Error-prone (manual length and file descriptor handling).
2. High-Level Version (with printf)
#include <stdio.h>
int main() {
printf("Hello, World\n");
return 0;
}
🧰 Characteristics:
-
Standard C Library:
printf()is part of the standard C library (libc), and it is portable across platforms that support ANSI C. -
Buffered output: Uses internal buffering; data may not appear immediately unless flushed.
-
Formatting capabilities: Extremely flexible for formatted output.
✔ Pros:
-
Cross-platform and portable.
-
Easy to use, readable, and maintainable.
-
Supports complex formatting.
✘ Cons:
-
Slightly slower due to buffering and formatting.
-
Might delay output unless flushed or newline is used.
🧠 So, Which One is "Better"?
It depends entirely on your context and goals:
| Context | Preferred |
|---|---|
| System programming, embedded systems, or you need precise control over I/O | write() |
| General C programming, teaching, portability, higher abstraction | printf() |
If you’re writing standard C applications or teaching beginners, printf() is unequivocally the correct choice. But if you’re writing operating system code, shells, or performance-critical utilities, write() might be more appropriate.
🧨 Pedantic Notes:
-
The
13inwrite(1, ..., 13)is not dynamically computed. If you change the string, you must update the number, or you risk undefined behavior or garbage output. -
printf()introduces additional runtime overhead but only marginally—insignificant in most applications.
💡 Recommendation:
Use printf() unless you explicitly need the unbuffered, minimalistic, low-level control that write() provides. High-level abstractions exist for a reason: they're safer, clearer, and portable. Use low-level tools only when you're solving low-level problems.

浙公网安备 33010602011771号