Intersting things about escape sequences
Escape sequence provides an general and extensible mechanism for representing hard-to-type or invisible characters. In C language escape sequences always begin with a backslash (\).
Charater constants represented by escape sequences like '\n' (newline) look like two characters, but represent only one.
There are fundamentally two distinct purposes behind the escape sequences.
1. The pricipal purpose of escape sequences is to represent invisible characters which control the motions of a printing device when they are sent to it.
e.g.
i. \f - Form feed: Go to the first position on the ‘next page’, whatever that may mean for the output device.
The Standard carefully avoids mentioning the physical directions of movement of the output device which are not necessarily the top to bottom, left to right movements commonly followed.
ii. Escape sequences also ease the way of representing some special characters that would otherwise be hard to get into a character constant (or hard to read; does ' ' contain a space or a tab?).
iii. An interesting thing about escape sequences is that it is also possible to use numeric escape sequences to specify a character in terms of the internal value used to represent it. This would be particularly useful in strings. A sequence of either \ooo or \xhhhh, where the ooo is up to three octal digits and hhhh is any number of hexadecimal digits respectively.
A common version of it is '\033', which is used by those who know that on an ASCII based machine, octal 33 is the ESC (escape) code.
The statement
printf("hello world\n");
could be well represented by using numeric escape sequence
printf("\x68\x65\x6c\x6c\x6f \x77\x6frld\xa");
Beware that the hexadecimal version will absorb any number of valid following hexadecimal digits; if you want a string containing the character whose value is hexadecimal ff followed by a letter f, then the safe way to do it is to use the string joining feature:"\xff" "f"The string"\xfff" only contains one character, with all three of the fs eaten up in the hexadecimal sequence.
2. It's obviously necessary to be able to represent characters such as single quote and backslash unambiguously: that is other purpose behind the escape sequences.
To get a single quote as a character constant you type '\''. To get a question mark you may have to use '\?'; not that '?' won't work, but to get two of them in there you can't use '??', because the sequence ??' is a trigraph! You would have to use '\?\?'. The escape \" is only necessary in strings.
Charater constants represented by escape sequences like '\n' (newline) look like two characters, but represent only one.
There are fundamentally two distinct purposes behind the escape sequences.
1. The pricipal purpose of escape sequences is to represent invisible characters which control the motions of a printing device when they are sent to it.
e.g.
i. \f - Form feed: Go to the first position on the ‘next page’, whatever that may mean for the output device.
The Standard carefully avoids mentioning the physical directions of movement of the output device which are not necessarily the top to bottom, left to right movements commonly followed.
ii. Escape sequences also ease the way of representing some special characters that would otherwise be hard to get into a character constant (or hard to read; does ' ' contain a space or a tab?).
iii. An interesting thing about escape sequences is that it is also possible to use numeric escape sequences to specify a character in terms of the internal value used to represent it. This would be particularly useful in strings. A sequence of either \ooo or \xhhhh, where the ooo is up to three octal digits and hhhh is any number of hexadecimal digits respectively.
A common version of it is '\033', which is used by those who know that on an ASCII based machine, octal 33 is the ESC (escape) code.
The statement
printf("hello world\n");
could be well represented by using numeric escape sequence
printf("\x68\x65\x6c\x6c\x6f \x77\x6frld\xa");
Beware that the hexadecimal version will absorb any number of valid following hexadecimal digits; if you want a string containing the character whose value is hexadecimal ff followed by a letter f, then the safe way to do it is to use the string joining feature:"\xff" "f"The string"\xfff" only contains one character, with all three of the fs eaten up in the hexadecimal sequence.
2. It's obviously necessary to be able to represent characters such as single quote and backslash unambiguously: that is other purpose behind the escape sequences.
To get a single quote as a character constant you type '\''. To get a question mark you may have to use '\?'; not that '?' won't work, but to get two of them in there you can't use '??', because the sequence ??' is a trigraph! You would have to use '\?\?'. The escape \" is only necessary in strings.
Comments