about the escaper character in C programming language

本文详细介绍了C语言中使用的转义字符,包括其在字符串和字符常量中的作用及意义,并通过示例代码展示了如何使用这些转义字符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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_char
cc     escaper_char.c   -o escaper_char
root@vinco:/home/vinco/c# ./escaper_char
escaper_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] = 37
escaper_char[1] = 39
escaper_char[2] = 63
escaper_char[3] = 7
escaper_char[4] = 8
escaper_char[5] = 9
escaper_char[6] = 10
100 %s
root@vinco:/home/vinco/c# 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值