14.C language characters

In the actual development, programmers rarely expressed significance of the data, as the main character array can be formed when a string with a single character. In this section, we highlight the relevant knowledge of the relationship between the code and ASCII characters and integers.

Character type char, only single quotes '' surrounded not by double quotation marks "" surrounded. Only the strings by double quotes, "
" surround, not by single quotes '' surrounded.

Output characters from% c, using the output string% s.

A, ASCII code table

ASCII (American Standard Code for Information
Interchange, American Standard Code for Information Interchange) is a computer based on the Latin alphabet coding scheme, which is mainly used to display modern English and other Western European languages, it is now the most common single-byte coding scheme, It is equivalent to the international standard the ISO / to IEC
646.

ASCII code specification first published in 1967 and last updated in 1986, it contains 33 control characters and 95 printable characters (characters with special meaning can not be displayed).

1, ASCII control character (the character encoding: 0-31)

In the ASCII code table, encoding the first 32 characters are not used for printing, but rather are used to control the same peripheral device like a printer.

Decimal symbol Chinese interpretation Decimal symbol Chinese interpretation
0 NULL Null character 16 ACCORDING TO Data Link Escape
1 SOH Title start 17 DC1 Device Control 1
2 STX Start of Text 18 DC2 Control apparatus 2
3 ETX End text 19 DC3 Device Control 3
4 ROT End of Transmission 20 DC4 Device Control 4
5 ENQ ask 21 NAK Refused to accept
6 ACK Notified 22 SYN Synchronous idle
7 BEL bell 23 ETB End of transmission block
8 BS backspace 24 CAN cancel
9 HT Horizontal tab 25 IN Media interrupt
10 LF Linefeed 26 SUB replace
11 VT Vertical tab 27 ESC Escape character
12 FF Page key 28 FS File separator
13 CR Enter key 29 GS Group separator
14 SO Out of 30 RS Record separators
15 AND Importation 31 US Separator unit

2, ASCII printable characters (character code: 32-127)

A total of 95 printable characters.

1) 32 is a space.

2) 48 to 57 ten arabic numerals 0 to 9;

3) 65 to 26 90 to capital letters;

4) 97 - 122 26 lowercase letters;

5) to rest for a number of punctuation marks, the arithmetic symbols;

6) The first 127 characters represents the delete command on the keyboard.

Decimal symbol Chinese interpretation Decimal symbol Chinese interpretation
32 Blank 80 P Capital letter P
33 ! Exclamation point 81 Q Capital letter Q
34 " Double quotes 82 R Capital letter R
35 # Well No. 83 S Capital letter S
36 $ The dollar sign 84 T Capital letter T
37 % Percent sign 85 The Capital letter U
38 & versus 86 V Capital letter V
39 apostrophe 87 W Capital letter W
40 ( Left parenthesis 88 X Capital letter X
41 ) Right parenthesis 89 Y Capital letter Y
42 * Asterisk 90 FROM Capital letter Z
43 + plus 91 [ Left parenthesis
44 , comma 92 \ Backslash
45 - Hyphen or minus sign 93 ] Right bracket
46 . Period or decimal point 94 ^ Diacritic
47 / Slash 95 _ Underline
48 0 0 96 ` Accent
49 1 1 97 a Lowercase a
50 2 2 98 b Lowercase b
51 3 3 99 c Lowercase c
52 4 4 100 d Lowercase d
53 5 5 101 e Lowercase e
54 6 6 102 f Lowercase f
55 7 7 103 g Lowercase g
56 8 8 104 h Lowercase h
57 9 9 105 i Lowercase i
58 : colon 106 j Lowercase j
59 ; semicolon 107 k Lowercase k
60 < Less than 108 l Lowercase l
61 = equal sign 109 m Lowercase m
62 > more than the 110 n Lowercase n
63 ? question mark 111 O Lowercase o
64 @ Email symbol 112 p Lowercase p
65 A A capital letter 113 q Lowercase letters q
66 B Capital letter B 114 r Lowercase r
67 C Capital letter C 115 s Lowercase s
68 D Capital letter D 116 t Lowercase t
69 E Uppercase E 117 in Lowercase u
70 F Capital letter F 118 v Lowercase v
71 G Capital letter G 119 w Lowercase w
72 H Capital letter H 120 x Lowercase x
73 I Capital letter I 121 Y Lowercase y
74 J Capital letter J 122 from Lowercase z
75 K Capital letter K 123 { Left brace
76 L Capital letter L 124 | Vertical line
77 M Capital letter M 125 } Right brace
78 N Capital letter N 126 ~ Tilde
79 O Capital letter O 127 delete

3, the escape character

对于 ASCII
编码,0~31(十进制)范围内的字符为控制字符,它们都是看不见的,不能在显示器上显示,也无法从键盘输入,C语言又定义了一种简单的书写方式,即转义字符的形式来表示。

转义字符完整的列表如下:

转义字符 意义 ASCII码值 使用频率
\n 换行(LF) ,将当前位置移到下一行开头。 10 每天都用
\’ 单引号。 39 常用
\" 双引号。 34 常用
\\ 反斜杠。 92 常用
\r 回车(CR) 13 windows平台常用,linux平台不常用。
\t 水平制表(HT) 。 9 20年前常用,现在不用。
\v 垂直制表(VT)。 11 20年前常用,现在不用。
\a 响铃(BEL)。 7 20年前常用,现在不用。
\b 退格(BS) ,将当前位置移到前一列。 8 20年前常用,现在不用。
\f 换页(FF),将当前位置移到下页开头。 12 20年前常用,现在不用。

\n是最常用的转义字符,表示换行,让文本从下一行的开头输出,前面的章节中已经多次使用。

\r\n用于windows平台DOS格式文件的换行。

单引号、双引号、反斜杠是特殊的字符,不能直接表示。

单引号是字符类型的开头和结尾,要使用\'表示。

双引号是字符串的开头和结尾,要使用\"表示。

反斜杠是转义字符的开头,要使用[\\表示](file:///\表示)。

示例(book67.c)

/*
 * 程序名:book67.c,此程序演示C语言的转义字符。
 * 作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>

int main()
{
  printf("输出'单引号\'的方法。\n");
  printf("输出双引号\"的方法。\n");
  printf("输出反斜杠\\的方法。\n");
  printf("单引号'不转义也能输出。\n");

  return 0;
}

运行效果

在这里插入图片描述
二、字符就是整数

**字符和整数没有本质的区别。**可以给 char
变量一个字符,也可以给它一个整数;反过来,可以给 int
变量一个整数,也可以给它一个字符。

char 变量在内存中存储的是字符对应的 ASCII 码值。如果以 %c 输出,会根据 ASCII
码表转换成对应的字符,如果以 %d 输出,那么还是整数。

int 变量在内存中存储的是整数本身,如果以 %c 输出时,也会根据 ASCII
码表转换成对应的字符。

也就是说,ASCII 码表将整数和字符关联起来了。

char类型占内存一个字节,signed char取值范围是-128-127,unsigned
char取值范围是0-255。

如果整数大于255,那么整数还是字符吗?

描述再准确一些,在char的取值范围内(0-255),字符和整数没有本质区别。

字符肯定是整数,0-255范围内的整数是字符,大于255的整数不是字符。

示例(book68.c)

/*
 * 程序名:book68.c,此程序演示字符与整数的关系
 * 作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>

int main()
{
  char a =  'E';
  char b =  70;
  int  c =  71;
  int  d = 'H';

  printf("a=%c, a=%d\n", a, a);
  printf("b=%c, b=%d\n", b, b);
  printf("c=%c, c=%d\n", c, c);
  printf("d=%c, d=%d\n", d, d);
}

运行效果

在这里插入图片描述

在ASCII码表中,E、F、G、H 的值分别是 69、70、71、72。

三、常用的库函数

以下是常用的字符函数,必须掌握。

int isalpha(int ch);  // 若ch是字母('A'-'Z','a'-'z')返回非0值,否则返回0。
int isalnum(int ch);  // 若ch是字母('A'-'Z','a'-'z')或数字('0'-'9'),返回非0值,否则返回0。
int isdigit(int ch);  // 若ch是数字('0'-'9')返回非0值,否则返回0。
int islower(int ch);  // 若ch是小写字母('a'-'z')返回非0值,否则返回0。
int isupper(int ch);  // 若ch是大写字母('A'-'Z')返回非0值,否则返回0。
int tolower(int ch);  // 若ch是大写字母('A'-'Z')返回相应的小写字母('a'-'z')。
int toupper(int ch);  // 若ch是小写字母('a'-'z')返回相应的大写字母('A'-'Z')

以下是不常用的字符函数,极少使用,了解即可。

int isascii(int ch);  // 若ch是字符(ASCII码中的0-127)返回非0值,否则返回0。
int iscntrl(int ch);  // 若ch是作废字符(0x7F)或普通控制字符(0x00-0x1F),返回非0值,否则返回0。
int isprint(int ch);  // 若ch是可打印字符(含空格)(0x20-0x7E)返回非0值,否则返回0。
int ispunct(int ch);  // 若ch是标点字符(0x00-0x1F)返回非0值,否则返回0。
int isspace(int ch);  // 若ch是空格(' '),水平制表符('/t'),回车符('/r'),走纸换行('/f'),垂直制表符('/v'),换行符('/n'),返回非0值,否则返回0。
int isxdigit(int ch); // 若ch是16进制数('0'-'9','A'-'F','a'-'f')返回非0值,否则返回0。

四、课后作业

1)研究ascii码表,重点关心几个问题:

(1)字符是整数,整数可以进行加减运算,字符可以吗?

(2)字母’A’-‘Z’、‘a’-‘z’和数字’0’-'9’的ascii码值是不是连续的?

(3)字母’A’和’a’、'Z’和’z’的ascii码值的差是多少?

2)根据上面的研究结果,编写函数,实现字符操作常用的库函数的功能,函数的声明如下:

int ISALPHA(int ch);   // 若ch是字母('A'-'Z','a'-'z')返回非0值,否则返回0。
int ISALNUM(int ch);   // 若ch是字母('A'-'Z','a'-'z')或数字('0'-'9'),返回非0值,否则返回0。
int ISDIGIT(int ch);   // 若ch是数字('0'-'9')返回非0值,否则返回0。
int ISLOWER(int ch);   // 若ch是小写字母('a'-'z')返回非0值,否则返回0。
int ISUPPER(int ch);   // 若ch是大写字母('A'-'Z')返回非0值,否则返回0。
int TOLOWER(int ch);   // 若ch是大写字母('A'-'Z')返回相应的小写字母('a'-'z')。
int TOUPPER(int ch);   // 若ch是小写字母('a'-'z')返回相应的大写字母('A'-'Z')

3)自定义一个函数,函数名是ctoi,利用ASCII码进行加减运算,把字符的’0’、‘1’、‘2’、‘3’、‘4’、‘5’、‘6’、‘7’、‘8’、'9’转换为整数的0、1、2、3、4、5、6、7、8、9。不允许用if和switch语句,只能用ASCII码运算,函数的声明如下:

int ctoi(const char chr);  // chr为用字符方式表示的数字,函数的返回值为数字的整数。

调用示例:

printf("'0' is %d\n",ctoi('0'));    // 输出结果是'0' is 0
printf("'9' is %d\n",ctoi('9'));    // 输出结果是'9' is 9

4)编写示例程序,测试char和unsigned char赋值超过取值范围的后果。

5)%不是转义字符,要输出%怎么办?

九、版权声明

C语言技术网原创文章,转载请说明文章的来源、作者和原文的链接。
来源:C语言技术网(www.freecplus.net)
作者:码农有道

发布了29 篇原创文章 · 获赞 2 · 访问量 682

Guess you like

Origin blog.csdn.net/m0_45133894/article/details/104638120