Take the 2-minute tour ×

Very often I get into C code where printf format strings start with \n:

printf( "\nHello" );

This in my opinion is an annoying thing that offers no advantages (rather many disadvantages!) with respect to printing "Hello\n":

  • If the first printed line begins with '\n', program output will begin with a (useless) empty line
  • If the last printed line doesn't end with '\n', program output won't end with a new line (useful when reading the output on a terminal)
  • On most terminals (on line buffered streams in general), output gets flushed when a '\n' is encountered, so a line not ending with '\n' could be shown on screen much time after it's been actually printf'd (or maybe never, if the stream never gets flushed, for instance if the program crashes)

So, why do people does like this?

share|improve this question
This is a little thing that really annoys me. People who do cout << endl << ..; also drives me mad. I know there's no difference but ... –  Vitor Jun 28 '11 at 16:14
Do you mean "flushing" in the sense of "new line" or as in fflush()? –  LennyProgrammers Jun 28 '11 at 16:22
@Lenny222: in the sense of fflush(). –  peoro Jun 28 '11 at 17:21
Ah, haven't realized, that printf() flushes on newlines, in contrast to C++ streams. –  LennyProgrammers Jun 29 '11 at 7:59
add comment

3 Answers

up vote 14 down vote accepted

Generally, it is done to ensure that the statement is printed on the next line. If it is done at the end of the line, the same effect can be derived. It really is of little consequence.

Update: As long as you pick one way and stick with it, it really is not going to matter one bit. If you are really concerned then type all your statements as "\nHello\n". If you do have some lines mashing together, then this is really not that hard of a "bug" to fix. Just go back and change the offending statement.

share|improve this answer
The difference is if you put it at the start of the line you guarantee that it will print out on a new line. If you're working on a big codebase there may be a line printing out before yours that doesn't have a newline being appended to it. –  Robert Anton Reese Jun 28 '11 at 15:37
This is particularly true if you are sticking in print statements for debugging purposes. –  Peter Rowell Jun 28 '11 at 15:47
Ok, but in this way a new message ("Hello2\n") could end up on the same line of your last one (ending up in "\nHelloHello2\n"): isn't this as bad as having your message on the same line of the previous one (eg: "\nPrevious-messageHello\n")? If these two scenarios are bad in the same way, a programmer/project should adopt a standard (eg: always putting '\n' at the beginning, or at the end), but I can't see what advantages you could have by putting '\n' at the beginning of your messages... –  peoro Jun 29 '11 at 0:21
@peoro, I think my update should offer a solution if it really bothers you. –  Morgan Herlocker Jun 30 '11 at 13:43
add comment

Two words: personal preference. In the grand scheme of things, I don't think this really matters. If you're annoyed by it, ask the author of this code why they write it like that. You may get some interesting answers.

I prefer all of my newline characters at the end of each line anyway.

share|improve this answer
+1 - It is all preference. I prefer my newline at the end of the string as well because it seems more natural to me –  Jetti Jun 28 '11 at 15:29
add comment

This will cause Hello to only be flushed after the next printf, which in my opinion is an annoying thing that offers no advantage.

Why do you think this is so? There's nothing special about the '\n' character.

Are you coming from C++ where std::endl flushes the stream? That's because std::endl is an object that can do things. The '\n' character doesn't do the same thing.

share|improve this answer
there is, the C99 standard says that an interactive stream (eg stdout) will not be fully buffered, which means \n will cause a flush. If you redirect it to a file, then you'll get different behaviour. –  gbjbaanb Jun 28 '11 at 16:48
Actually, most terminals (even in C++03 and C89) do use line buffering, which means the '\n' character is special. –  Billy ONeal Jun 28 '11 at 17:46
Shouldn't this be a comment, not an answer? –  Wipqozn Jun 28 '11 at 18:19
add comment

Your Answer


