A little bit more...

Wednesday, July 25, 2007

Tips of Using Escape Characters

First, what is the differences between '\n' and '\r\n'?

Cite here: )

It goes back to mechanical teletypes; if you issued a line feed followed by carriage return you got a few characters printed backwards in a diagonal stripe because there was insufficient time for the carriage to move back to the left before it started printing. To overcome this, the carriage return is issued first to give the carriage enough time to return, whilst line feeding, before printing started. It has stuck as a standard ever since.
 
and here:

Different Operating Systems handle newlines in a different way. Here is a short list of the most common ones:
  • DOS and Windows

    They expect a newline to be the combination of two characters, namely '\r\n' (or 13 followed by 10).

  • Unix (and hence Linux as well)

    Unix uses a single '\n' to indicate a new line.

  • Mac

    Macs use a single '\r'.
When string are read from user input, escape character can't be explicitly input. For example, in case which string are read from the command line argument, when the user give argument "hello\nworld", the string read in and stored will be "hello\\nworld". This is because escape characters usually are parsed as format presentation rather than literal  occurrence (actually, this is the "unescapted" meaning). For the same reason, in the programming codes escape characters, the literal form of format presentation, are used instead of draw a format presentation that the compiler could understand, which is obviously of non-sense.

Note that using escape characters to specify the format presentation is not the definition of escape character, though in many cases they're used in this way.

No comments:

About Me

My photo
I'm finishing my master degree in Software Engineering, Computer Science. I believe and have been following what Forrest Gump's Mam said: you have to do the best with what god gave you.