Tips on Displaying Special Characters in C++ Programs


The ASCII-8 language used to represent text data on most desktop computers has the capacity to represent 256 different characters. Some of these characters, such as letters, numerals, and punctuation, are symbols that are visible on computer keyboards and can be displayed on computer screens. Some are not actually symbols, but rather control codes such as the backspace, tab, carriage return, and the alert tone (bell). They are not visible symbols, but rather effect the layout of text, and so, need to be stored and sent to output devices. Some characters do not even have a key on the keyboard, making them difficult to produce. Examples of such characters are the copyright symbol (©) and the trademark symbol (™). Some of these "extended characters" can be produced by pressing combinations of keys on the keyboard. Others can be produced by software using special instructions called escape sequences. For example, you can generate an alert tone (bell) in a C++ program by including the escape sequence "\a" in an output statement. See the table below for a list of typical escape sequences used in C++ programs.

Most languages have special symbols that are used within their source code to indicate specific actions to be performed by the compiler when translating the source code into object code (machine language). For example, the quote mark is used in many languages to indicate the start of literal text. Thus the quote symbol cannot be output in a normal string without adding something to the statement to indicate a non-standard use of that symbol within the source code. The solution is to represent the troublesome character (the quote in this case) in another way than simply typing it. For this purpose, C++ contains special instructions called escape sequences, which are sequences of characters that start with the symbol "\" (backwards slash) and end with one or more symbols that represent the special character to be displayed. For example, the statement to produce the following console output:

Project "A1":

would be

    cout << "Project \"A1\":\n";

Notice the use of the backslash (\) character ahead of the quote (") symbols to "escape" from the normal output stream to indicate that the special quote symbols should be treated literally instead. Sometimes an escape sequence ends with a different symbol than the one to be displayed because that symbol is a control code such as the tab or new line character. You can see an example of the new line character being represented near the end of the statement above as "\n". A list of the most frequently used escape sequences appears in the table below.


Table of C++ Escape Sequences
Escape
Code
ASCII
Character
Meaning
\a<BELL>Ring the bell (Alert)
\b<BSL>Backspace
\f<FF>Form Feed - Advance to Next Page
\n<LF>Line Feed - New Line
\r<CR>Carriage Return (but no <LF>)
\t<TAB>Tab to Next Position
\\\Backslash ("Whack")
\''Apostrophe ("Single Quote")
\""Quote ("Double Quote")
\0<NUL>Null Character (End of String)

PATH: Instructional Server> COP 2000>