Cplusplus 简明教程
C++ Character (char) Data Type
C++ 中的 character (char) data type 表示字母数字值,这些值可以包含各种字符。这些字符可能包括字母,例如“a”、“b”和“c”,数字值,例如“1”、“2”和“3”,符号,例如“#”、“$”和“&”,以及更多内容。
The character (char) data type in C++ stands for alphanumeric values, which can be a wide range of characters. These may include alphabets like 'a', 'b', and 'c', numeric values like '1', '2', and '3', symbols like '#', '$', and '&', and many more.
character data type 占用 1 字节(即 8 位)的内存空间来存储字符。在 C++ 中,关键字“ char ”用于声明字符变量。
The character data type takes 1 Byte (i.e., 8 bits) of memory space to store characters. In C++, the keyword "char" is used to declare a character variable.
在该教程中,我们将探究有关字符数据类型的更多信息,及其相应的变量。
In this tutorial, we will explore more about character data type, and its corresponding variables.
Use Character (char) Data Type
以下是一些字符 (char) 数据类型的用法 −
The following are some of the uses of character (char) data type −
-
The char data type is used when we need to hold only a single character and do not need the overhead of String.
-
The char data type can also be used in primitive form as an array without the use of a string literal.
-
In ASCII form, char data type can be used to represent numeric values and vice-versa.
Values of char Data Type
C++ 中的字符 (char) 数据类型可以具有多个值,这些值如下 −
The character (char) data type in C++ can have multiple values, and these values are as follows −
-
Uppercase Alphabets, like A, B, Z, etc.
-
Lowercase Alphabets, like a, b, z, etc.
-
Symbols, like $, %, &, etc.
-
Escape Sequences, which will be discussed later in this article.
Creating a Character (char) Variable
我们可以使用关键字 “char” 后接变量名来声明一个字符变量。
We can declare a character variable using the "char" keyword followed by the variable name.
Example of Character (char) Data Type
以下示例展示了不同字符数据类型的用途 −
The following example shows the use of different character data types −
// C++ program to demonstrate
// Character data type
#include <iostream>
using namespace std;
int main() {
char ch,ch1,ch2;
ch='a'; //this is an alphabet
ch1='&'; //this is a symbol
ch2='1'; //this is a number
cout<<ch<<endl<<ch1<<endl<<ch2<<endl;
return 0;
}
ASCII Values of Characters
ASCII stands for the "American Standard Code for Information Interchange". It was the first set of encoding values assigned to different characters and symbols. The character sets used in modern computers, in HTML, and on the Internet, are all based on ASCII.
ASCII 表现所有字符类型的一个数值,可以使用这些值来声明字符,而不用显式使用字符本身。它包含数字 0-9、大写和小写英文字母 A 到 Z 以及一些特殊字符。
The ASCII table describes a numeric value for all character types, and these values can be used to declare a character without the explicit use of the character itself. It contains the numbers from 0-9, the upper and lower case English letters from A to Z, and some special characters.
以下数据给出了 C++ 中所有字符的 ASCII 值参考 −
The following data gives a reference for all ASCII values of characters available in C++ −
ASCII Range of 'a' to 'z' = 97-122
ASCII Range of 'A' to 'Z' = 65-90
ASCII Range of '0' to '9' = 48-57
Example to Show ASCII Declaration
以下示例展示了用户如何使用 ASCII 值声明字符变量,而不用显式使用字符本身 −
The following example shows how a user can declare a character variable using ASCII values, without explicit usage of the character itself −
#include <iostream>
using namespace std;
int main() {
char ch,ch1,ch2;
ch=65; //this is an alphabet
ch1=45; //this is a symbol
ch2=55; //this is a number
cout<<ch<<endl<<ch1<<endl<<ch2<<endl;
return 0;
}
A
-
7
Implicit Conversion of Character Variables
可以使用 ASCII 引用将字符变量隐式转换为其整数值,反之亦然。因此,当我们在 C++ 中声明一个字符时,我们可以引用其 ASCII 值,而 ASCII 数字也可以用来访问其字符值。这是使用数据类型的隐式转换或类型转换来完成的。
Character variables can be implicitly converted to their integer values using ASCII references, and vice-versa. Hence, when we declare a character in C++, we can reference their ASCII value, whereas an ASCII numerical can be used to access its character value as well. This is done using implicit conversion or typecasting of data types.
我们可以添加我们要将给定变量转换成的目标数据类型的关键字,然后编译器会自动更改数据类型。例如,如果我们编写 char(97),它将加载 ASCII 数字 97 的字符值,即“a”。将字符数据类型转换为整型(ASCII)值也是可行的。
We can add a keyword of the data type we need to convert the given variable into, and the compiler changes the data type automatically. For example, if we write char(97), it will load the character value of ASCII number 97, which is ‘a’. This is also possible for converting the character data type to integral (ASCII) values.
这在以下给出的示例中得到了明确解释:
This is clearly explained in the examples given below −
Example
以下示例展示了 char 到 int 的隐式类型转换,反之亦然 −
The following example shows the implicit typecasting of char to int, and vice-versa −
#include <iostream>
using namespace std;
int main() {
char c = '$';
int a = 97;
cout << "The Corresponding ASCII value of '$' : ";
cout << int(c) << endl;
cout << "The Corresponding character value of 97 : ";
cout << char(a) << endl;
return 0;
}
The Corresponding ASCII value of '$' : 36
The Corresponding character value of 97 : a
ESCAPE SEQUENCE IN C++
以反斜杠(“\”)开头的字符变量称为转义序列。它们确定编译器的输出窗口上的输出顺序。在此上下文中,反斜杠也称为“转义字符”。
Character variables which begin with a backslash (“\”) are called escape sequences. These determine on the output sequence on the output window of a compiler. In this context, backslash is also called as the ‘Escape Character’.
下表显示了 C++ 中的不同类型的转义序列 −
The following table shows different types of escape sequences available in C++ −
S. No. |
Escape Sequences |
Character |
1. |
\n |
Newline |
2. |
|Backslash |
3. |
\t |
Horizontal Tab |
4. |
\v |
Vertical Tab |
5. |
以下示例代码清楚地说明了转义序列的使用 −
The usage of escape sequences is clearly explained in the following example code −
Example 1
#include <iostream>
using namespace std;
int main() {
char a = 'H';
char b = 'E';
char c = 'Y';
char d = '\n'; //enter new line
char e = '\t'; //tab to enter space
cout << a << b << c << d << a << b << c << e << a << b << c;
return 0;
}
HEY HEYHEY
HEY HEY
转义字符还可用于在字符串中插入 special characters 。以下给出的示例中清楚地说明了这一点 −
The escape character can also be used to insert special characters into strings. This is clearly explained in the example given below −