Jump to content

do we always surround escape sequences such as \n and \t by quotation marks?

- - - - -

  • Please log in to reply
3 replies to this topic

#1
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Hi :)

Do we always surround escape sequences such as \n and \t by quotation marks? I think this is the case. But I'm almost sure that I've seen \n being used without any quotation marks. Could you please confirm it? Thank you.
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
\n is two separate characters and has no real meaning as it is an invalid name of a variable, and not a keyword in C.

The compiler however will detect \ as denoting an escape sequence in a string (conventionally other binary characters could be used that were not exactly printable as \) and uses the next characters as the sequence of the previous escape (n as in newline, r as in carriage return, t as in tab, x00 as in hexadecimal character, ....)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts

Alexander said:

\n is two separate characters and has no real meaning as it is an invalid name of a variable
Ew no it's not. \n is the Line Feed character (0x0A in ASCII) and is used on standards compliant UNIX systems to indicate a new line. The C standard requires that any compliant C compiler must produce a new line when it encounters a \n within a hardcoded string transparently, so compilers on operating systems such as Windows will replace \n with "\r\n" (carriage return + line feed), since that's what Windows uses. For Mac OS up to OS 9, it's replaced with \r. This only occurs in text literal strings, if it's a binary literal string \n will produce only the single line feed character.

Simply put, it is not a variable, but it certainly does have a real meaning and is not two separate characters, only frequently output as two separate characters by the compiler for the system they are on.
Wow I changed my sig!

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
That is incorrect (based on the compilers we use, of which implement the various standard). The lexer will read the string literal token '\' and then the following character(s) as I have mentioned which is correct to assume in his asking, this includes the \xuuuu added in C99 as I have hinted above. In fact most of the conforming compilers on Windows will store the character in an ignored pointer (non returning token) and iterate the line with ASCII 012 (newline), and will not place a carriage return unless you exactly specify \r\n (and it is incorrect to do so, you can observe this yourself too)

If there are standards that denote otherwise, It would be informative to both of us however if you could show them.

Edited by Alexander, 02 June 2011 - 08:12 PM.

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users