NCL 208: Special Characters
So far, most of the characters in our strings have been written directly into the program.
If we want:
Hello!
we can write:
D.TXT "Hello!"
D.BLT
But some characters are difficult to write directly inside a string.
Others aren't normally visible at all.
NCL uses escape sequences to represent these characters.
Putting quotes inside a string
Suppose we want to display:
She said "Hello!"
We can't write:
D.TXT "She said "Hello!""
because the quotation mark already has a job in NCL: it marks the beginning or end of a string literal.
Instead, we can put a backslash before a quotation mark:
D.TXT "She said \"Hello!\""
D.BLT
The program displays:
She said "Hello!"
Inside a string, \" represents one quotation mark.
The combination of the backslash and the character following it is called an escape sequence.
Writing a backslash
Since the backslash introduces an escape sequence, it also needs an escape of its own.
To put one backslash in a string, write two in the source:
D.TXT "SYSTEM\\STARTUP"
D.BLT
The program displays:
SYSTEM\STARTUP
The source contains \\, but the resulting string contains one backslash.
Escape sequences can also represent characters that aren't normally visible.
Carriage return and line feed
Consider:
D.TXT "Hello\r\nWorld"
D.BLT
The Display shows:
Hello
World
There are two different characters between the words.
\r is carriage return. On the Display, it moves the cursor to the beginning of the current row.
\n is line feed. It moves the cursor down one row.
Together, \r\n moves the cursor to the beginning of the next row.
The difference becomes clearer if we leave the carriage return out:
D.TXT "ONE\nTWO\nTHREE"
D.BLT
Line feed moves the cursor down, but doesn't move it back to the left.
The result forms a staircase:
ONE
TWO
THREE
Carriage return and line feed are separate characters because they perform separate operations.
Why are they called carriage return and line feed?
The names come from mechanical teleprinters and typewriters.
Carriage return returned the printing carriage to the beginning of the line.
Line feed advanced the paper by one line.
They were physically different operations, which is why computing inherited two different control characters for them.
The names survived long after most computers stopped moving actual carriages and feeding actual paper.
Other control characters
Carriage return and line feed belong to a group of characters called control characters.
Rather than displaying a visible glyph, a device may interpret a control character as an instruction.
NCL provides escapes for several of them:
| Escape | Name | Display effect |
|---|---|---|
\r |
Carriage return | Move to the beginning of the current row |
\n |
Line feed | Move down one row |
\t |
Horizontal tab | Move to the next tab stop |
\b |
Backspace | Move back one character |
\f |
Form feed | Move the cursor to (0,0) |
\d |
Delete | Blank the character at the cursor without moving |
\a |
Bell | Sound a beep |
These characters can exist inside strings just like visible characters.
For example:
S.MOVE #message "READY\a"
stores a string containing the five visible letters in READY followed by a bell character.
Nothing needs to happen when the string is stored.
The effect occurs when something that gives the control character meaning receives it.
What's actually in the string?
An escape sequence is how we write a character in NCL source.
The escape itself isn't stored as several characters.
Consider:
S.MOVE #message "A\nB"
S.LEN #length #message
#length receives:
3
The string contains:
A
[line feed]
B
The two source characters \ and n were a way of specifying one line-feed character.
This distinction matters whenever we measure, search, extract, or otherwise manipulate strings.
Characters and their effects
A character doesn't have to display something itself.
For example:
S.MOVE #message "HELLO\r\nWORLD"
simply stores a string.
The carriage return and line feed are characters inside that string.
When we later send it to the Display:
D.TXT #message
D.BLT
the Display interprets those characters and moves its cursor accordingly.
This is why it is useful to distinguish the character in the string from the effect a device gives that character.
The same string can be stored, copied, joined, searched, or passed somewhere else without those control characters doing anything by themselves.
Try it
Create a string containing three words separated only by line feeds:
ONE
TWO
THREE
Display it and observe the staircase effect.
Then change the separators to carriage return followed by line feed so the words begin in the same column.
Store the completed string in memory before displaying it.
Use S.LEN to measure it, and predict the result before running the program.
Remember that each escape sequence represents one character in the resulting string.
For an extra experiment, add \a to the end of the string.
Does storing the string sound the bell?
Does displaying it?
Escape sequences let us write characters that would otherwise be awkward or invisible in NCL source.
Once the program is running, the escape sequence has done its job. The string contains the character it represented.
In NCL 209: Unicode, we'll look at how those characters are identified and represented.