Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printf: Difference between \n and %n [duplicate]

I'm reading Effective Java and it uses %n for the newline character everywhere. I have used \n rather successfully for newline in Java programs.

Which is the 'correct' one? What's wrong with \n ? Why did Java change this C convention?

like image 920
ripper234 Avatar asked Feb 14 '26 03:02

ripper234


1 Answers

From a quick google:

There is also one specifier that doesn't correspond to an argument. It is "%n" which outputs a line break. A "\n" can also be used in some cases, but since "%n" always outputs the correct platform-specific line separator, it is portable across platforms whereas"\n" is not.

Please refer https://docs.oracle.com/javase/tutorial/java/data/numberformat.html

Original source

like image 107
Bill K Avatar answered Feb 16 '26 19:02

Bill K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!