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.
init()
andprocess_data()
print anything themselves? What would you expect the result to look like if they did?\n
is a line terminator, not a line separator. This is evidenced by the fact that text files, on UNIX, almost always end in\n
.