Should I use printf(“\n”) or putchar(‘\n’) to print a newline in C?

It will make no difference which one you chose if you’re using a modern compiler[1]. Take for example the following C code.

#include <stdlib.h>
#include <stdio.h>

void foo(void) {

void bar(void) {

When compiled with gcc -O1 (optimizations enabled), we get the following (identical) machine code in both foo and bar:

movl    $10, %edi
popq    %rbp
jmp _putchar                ## TAILCALL

Both foo and bar end up calling putchar('\n'). In other words, modern C compilers are smart enough to optimize printf calls very efficiently. Just use whichever one you think is more clear and readable.

  1. I do not consider MS’s cl to be a modern compiler.

Leave a Comment