Guides:C/C Crash Course/Escape Codes

From CoderGuide

Jump to: navigation, search

Back to TOC

Escape Codes

You're going to be encountering a lot of escape codes in this section of the form \[some letter]. Until now, I've pretty much been avoiding the subject to discuss other topics, but now seems like a good time to discuss them in more detail. There are many characters you can't type on your keyboard that you can't otherwise use in your program, such as, how do you put a newline character in a string? Press the return key? How about issuing the bell or backspace character? What if you want to use the double quote character in your string? Well, we use escape codes to do this. And here is a description of these escape codes:

CodeMeaning
\\A single backslash
\nnewline
\rcarriage return
\aBell
\bbackspace
\tTab
\"double-quote
\[newline]treats two lines of text as one
\NNNoctal (base-8) code for a character
\xNNhexadecimal (base-16) code for a character
\fform feed
\vvertical tab
\0The null character, which has a value of zero

The bell character, when printed to the screen or an old line printer (the dot matrix or daisy-wheel printers we all used to use ages ago) will usually cause the terminal, or printer, to emit a beep or the terminal may flash instead (or both beep and flash). The bell can be thought of as an "alert" to the user.

The backspace character, when sent to a line printer, or the screen, will move the cursor back one space and usually leaves the text under the character in place.

The tab character will move the cursor over to the next tab stop, which are, by default, ever eight spaces, and so there are ten tab stops on a standard 80-column display.

The form feed character, if sent to a printer, tells the printer to spit out whatever page it currently is on, or spit out a new page if it's not printing on any page. On some systems, the form feed character, if sent to a terminal screen, will clear the screen. Other times, it will do nothing. On PC systems running DOS or windows, it will usually print the Greek symbol for female on the screen. You're not likely to ever need to use this at all.

The carriage return character tells the terminal, and line printer, to move the "carriage", or, in our case, the cursor, back to the beginning of the line. It does not move the cursor down a line.

The newline character moves the cursor down one line. Unix will send a carriage return to the terminal automatically if a new line character is sent to it. C/C++ on Windows systems, will add the newline character when writing to a file in text mode, or when writing to the screen. So, all you need to do, if you want to print on a nice new, clean line of text, is issue the newline character.


You can find out more about character codes by referring to: Reference:ASCII Table

And that pretty much wraps it up for escape codes.

Personal tools