Escape characters in C Language

Source: Internet
Author: User
Tags printable characters

Escape characters in C Language


A character set has the following characteristics: When you enter this character on the keyboard, the display displays this character, that is, what you enter. These characters are calledPrintable charactersSuch as A, B, C, $, +, and space characters.
Another type of character does not have this feature. Either they cannot find a corresponding key on the keyboard (you can enter it in a special way), or they cannot display the characters on the key surface after the key is pressed. In fact, these types of characters are designed for control purposes.Control characters.
In C,Control characters that constitute character constants must be represented by escape characters. An escape character starts. For example, the escape character is represented by '\ B', And the linefeed is represented by '\ n.'\'Indicates that the character after it has lost its original meaning and is converted into another specific meaning.A backslash is a specific character along with the character following it.
Escape characters are a special form of characters in C. Escape characters with backslashes'\', Followed by a character or an octal or hexadecimal number. The escape character has a specific meaning. It is called an escape character because it is different from the original meaning of the character.
Generally, escape characters are used to indicate non-printable control characters in the ASCII character set and special function characters. For example, a single marker (') is used to represent a character constant ('), used to represent the double apostrophes (") and backslash (\. For example,' \ N' is an escape character, which means "carriage return ". Escape characters are mainly used to indicate the control that is not easily expressed by general characters.Code. The following table showsCommon escape characters and their meanings:



Escape characters Meaning ASCII value (decimal)
\ Bell (BEL) 007
\ B Return to BS to move the current position to the previous column. 008
\ F Change page (ff), move the current position to the beginning of the next page 012
\ N Line feed (LF), move the current position to the beginning of the next line 010
\ R Press enter (CR) to move the current position to the beginning of the line 013
\ T Horizontal tabulation (HT) (jump to the next tab) 009
\ V Vertical tabulation (VT) 011
\\ Represents a backslash character ''\' 092
\' Represents a single quotation mark (apostrophes) character 039
\" Represents a double quotation mark character 034
\ 0 Null) 000
\ Ddd Any character from 1 to 3 octal digits Three octal nodes
\ Xhh Any character in hexadecimal notation between 1 and 2 Two hexadecimal

It can be seen from the table that there are three escape characters in the C language: General escape characters, octal escape characters and hexadecimal escape characters.

General escape characters

This escape character, although formally composed of two characters, only represents one character. The common escape characters are:
\A\N\T\V\B\R\F\\ \'\"

Careful readers may have discovered that the escape character'\/'Indicates the Backslash "/" and escape character'\''Represents the character" '"And escape character'\"'Represents the character" ", which is itself a printable character. Why should we escape it?
This is because their original character form has been used for it, where single quotation marks are used to distinguish character constants and double quotation marks are used to distinguish strings (strings will be introduced below, the backslash itself is used to indicate the beginning of the escape character, so it must be declared again with the escape character.

Octal escape characters

It consists of the backslash '/' and the subsequent 1 ~ A string of three Octal numbers. For example ,'\60 ','\101 ','\141 characters: '0', 'A', and 'A '. Because the ASCII values of '0', 'A', and 'A' are 60, 101, and 141, respectively. All characters in the character set can be represented by octal escape characters. If you want to, you can add 0 in front of the octal number to indicate the octal transfer character.

Hexadecimal escape characters

It consists of the backslash '/' and the letter X (or X) and the subsequent 1 ~ A string of two hexadecimal numbers. For example ,'\X30 ','\X41 ','\The x61 characters are '0', 'A', and 'A' respectively '. The hexadecimal values of the ASCII codes '0', 'A', and 'A' are 0x30, 0x41, and 0x61, respectively.
It can be seen that all characters in the character set can be expressed by hexadecimal escape characters.

We can see that the octal Escape Character and hexadecimal escape character can not only represent control characters, but also can also represent display characters. However, different computer systems may use different character sets.ProgramIt can be easily transplanted to other computer systems for running, and the program should use less escape characters in this form.

Note the following when using escape characters:
    • 1) only lowercase letters can be used in escape characters. Each escape character can only be considered as one character.
    • 2) The \ v vertical tabulation and the \ f tab have no impact on the screen, but will affect the printer's response.
    • 3) when non-printable characters are used in C Programs, escape characters are usually used.
    • 4) Escape Character '\ 0' indicates null, and its value is 0. The ASCII value of the character '0' is 48. Therefore, the null character '\ 0' is not the character 0. In addition, the null character is not equal to the space character. The ASCII value of the space character is 32 rather than 0. Readers should be clear about the differences during programming.
    • 5) if the character after the backslash does not constitute an escape character, '\' cannot escape and will be ignored.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.