about the escaper character in C programming language

来源:互联网 发布:淘宝拍a发b平台荷塘 编辑:程序博客网 时间:2024/05/16 07:57

forewords:

In the character set can be divided into two categories.
There is a class of characters can be displayed as characters, such as a, @, c, ^, #, and spaces and so on.
The other characters called control characters, is designed to control function, so called control characters.

In C, the escape character is used to indicate that the main character is not generally easy to use, said control code.
They can not find on the keyboard or a corresponding key (of course you can enter a special mode), or when the key is not after the characters on the display interface.

Escape character is a kind of "\" character, followed by a character or an octal (\ ddd) or hexadecimal (\ xhh) said.
Together with a backslash character after character to form a particular, specific meaning, different from the character of the original meaning (it is called the escape character).

1 followed by a character representation escape character, although in the form of two characters, but represent a character.General escape character is used: 

\ a \ n \ t \ v \ b \ r \ f \ \ \ '\ "

Escape character '\ \' on behalf of the backslash "\" escape character '\'-character "'" and the escape character' \ "'representative character" ""

Is to show the character of its own, why should it escape it?
This is because the form of their original character as it has been used, in which the distinction between single quotes for character constants in parentheses, quotation marks used to distinguish the string (the following will be
Introduction string) brackets, and the backslash escape character itself is used to indicate the beginning, so they must be re-statement with the escape character.

2 octal and hexadecimal escape characters

They are the backslash '\', respectively, and the subsequent 1 to 3 octal digits or letters x (or X) and subsequent 1 to 2 hexadecimal digits consisting of a sequence of characters.
For example,
'\ 170', '\ 171', '\ 172' or '\ x78', '\ x79', '\ x7A', respectively, the character 'x', 'y', 'z'.
Because the character 'x', 'y' and 'z' of the ASCII code of octal values ​​were 170, 171 and 172, respectively, hexadecimal value 0x78, 0x79, and 0x7A.

Characters using the octal and hexadecimal escape the escape character, not only that the control characters, but can also be said to display the character (if you wish).
However, due to different computer systems may use different character sets, so, in order to program portability, the program should be less use of this form of escape characters.

the escaper character sets  as following:

escaper charatcer

ASCII ( in Hexadecimal)

ASCII (in Decimal )

means

\0

0x00

0

NULL

\a

0x07

7

beep

\b

0x08

8

backspace

\t

0x09

9

horizontal tab

\n

0x0A

10

line feed

\v

0x0B

11

vertical tab

\f

0x0C

12

feed

\r

0x0D

13

carriage return

\"

0x22

34

double quotes

\'

0x27

39

single quotes

\?

0x3F

63

question mark

\\

0x5C

92

backslash

\ddd

 

 

 

\xhh

 

 

 

 

 in fact ,

1. "\?" is equalvalent to "?" , and " \' " is equalvalent to " ' " as well, it seem that they have no need to do escape ;

2. if you want to print "%"(it' not a escaper character) in printf(), you should add extra special "%",  in other words you shall use "%%" in the first parameter

(a string means the formatted outputprint ), otherwise , it's alert "segmentation fault" when run it (althought it pass the compiler).

it's the same as other series of print funciton , such as sprintf(). snprintf(), fprintf()...etc.

3. '\0' means the end of a string,if you use "strlen()" to count the character number of a string, it's end at "\0", and it excludes the character "\0";

 

/** escaper_char.c by vinco at 2011-09-03* Ubuntu CC/gcc-4.4.1*/#include<stdio.h>#include<string.h>int main(){char escaper_char[]="%'?\a\b\t\n\0\v\f\r\"\'\?\\";int i = 0;printf("escaper_char = %s\n",escaper_char);for(i = 0; i < sizeof(escaper_char); i++)printf("escaper_char[%d]\r = %d\n",i, escaper_char[i] );printf("\n");for(i = 0; i < strlen(escaper_char); i++)printf("escaper_char[%d] = %d\n",i, escaper_char[i] );printf("%d %%s\n",100);return(0);}


run it:

 

root@vinco:/home/vinco/c# make escaper_charcc     escaper_char.c   -o escaper_charroot@vinco:/home/vinco/c# ./escaper_charescaper_char = %'? = 37er_char[0] = 39er_char[1] = 63er_char[2] = 7per_char[3] = 8per_char[4] = 9per_char[5] = 10er_char[6] = 0per_char[7] = 11er_char[8] = 12er_char[9] = 13er_char[10] = 34er_char[11] = 39er_char[12] = 63er_char[13] = 92er_char[14] = 0per_char[15]escaper_char[0] = 37escaper_char[1] = 39escaper_char[2] = 63escaper_char[3] = 7escaper_char[4] = 8escaper_char[5] = 9escaper_char[6] = 10100 %sroot@vinco:/home/vinco/c# 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 幼儿园不收外省小孩怎么办 孩子刚上幼儿园哭闹老师怎么办 孕妇糖耐指标高怎么办 貂蝉经常没蓝怎么办 儿童声导抗c型怎么办 酱牛肉不烂怎么办拯救 卤牛肉一切就碎怎么办 牛肉煮的太烂了怎么办 牛肉卤的太烂怎么办 牛肉炖得太软怎么办 牛肉炖的很硬怎么办 牛肉做的有点硬怎么办 牛肉做老了还能怎么办 牛肉粒 炒老了怎么办 牛肉炖出来很硬怎么办 煮的牛肉太硬怎么办 在产蛋鸡体重偏轻怎么办 产蛋鸡不上高峰怎么办 蛋鸡天热下蛋少怎么办 成年鸡嗉子鼓涨怎么办 黄牛拼失败了钱怎么办 磁力泥粘衣服上怎么办 鼻涕泥弄衣服上怎么办 水晶橡皮泥弄在衣服上怎么办 橡皮泥弄到衣服上怎么办 橡皮泥弄在衣服上怎么办 磁力泥弄衣服上怎么办 苹果7p按键坏了怎么办 苹果手机内存满了怎么办 来微信消息手机上面没提示怎么办 苹果手机储存空间满了怎么办 苹果六储存满了怎么办视频 微信听筒没声音怎么办 苹果手机有电自动关机怎么办 白玉蜗牛生蛋了怎么办 农民工工资拖欠怎么办没有合同 公司拖欠员工工资没签合同怎么办 东京去大阪乘大巴行李怎么办? 在外手机没电了怎么办 被公司起诉我该怎么办 支付宝付款刷脸怎么办