C Programming – Why Use Trailing Newlines Instead of Leading with printf?

c

I heard that you should avoid leading newlines when using printf. So that instead of printf("\nHello World!") you should use printf("Hello World!\n")

In this particular example above it does not make sense, since the output would be different, but consider this:

printf("Initializing");
init();
printf("\nProcessing");
process_data();
printf("\nExiting");

compared to:

printf("Initializing\n");
init();
printf("Processing\n");
process_data();
printf("Exiting");

I cannot see any benefit with trailing newlines, except that it looks better. Is there any other reason?

EDIT:

I'll address the close votes here and now. I don't think this belong to Stack overflow, because this question is mainly about design. I would also say that although it may be opinions to this matter, Kilian Foth's answer and cmaster's answer proves that there are indeed very objective benefits with one approach.

Best Answer

A fair amount of terminal I/O is line-buffered, so by ending a message with \n you can be certain that it will be displayed in a timely manner. With a leading \n the message may or may not be displayed at once. Often, this would mean that each step displays the progress message of the previous step, which causes no end of confusion and wasted time when you try to understand a program's behaviour.