This question is with regards to ‘\n’ in C, C++ and Java. For each of the respective languages, is the ‘\n’ character platform dependent(LF on unix CRLF on windows) or it is only LF regardless of the platform
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In C and C++
\nis the newline character to be used in strings inside the application. The conversion to the platform-specific newline sequence is done by the IO streams when opened in text mode (i.e. not in binary mode).Notice that this is true for the standard library, but may not hold for particular APIs – e.g. I remember that some Windows controls needed an explicit CRLF to work correctly.
As for Java, I may be wrong (I almost never worked in Java), but from what I can see from the documentation the streams don’t perform any translation; instead, you can use
System.lineSeparator()to obtain the current platform’s line separator. Using this information,BufferedWriterprovides a handynewLinemethod to output a newline to the underlying stream.