Computer Programming 简明教程

Computer Programming - Characters

如果在计算机编程中处理数字很简单,那么处理字符将会更简单。字符就像 a、b、c、d……、A、B、C、D……,但有一个例外。在计算机编程中,任何个位数数字,如 0、1、2……和特殊字符,如 $、%、+、- ……等,也都视为字符,要将它们分配给字符类型变量,你只需将其放入 single quotes 中。例如,以下语句定义了一个字符类型变量 ch ,我们给它分配一个值 'a'−

If it was easy to work with numbers in computer programming, it would be even easier to work with characters. Characters are simple alphabets like a, b, c, d…​., A, B, C, D,…​.., but with an exception. In computer programming, any single digit number like 0, 1, 2,…​.and special characters like $, %, +, -…​. etc., are also treated as characters and to assign them in a character type variable, you simply need to put them inside single quotes. For example, the following statement defines a character type variable ch and we assign a value 'a' to it −

char ch = 'a';

在此, ch 是字符类型变量,可以容纳一个实现字符集中的字符,而 'a' 称为 character literal 或字符常量。不仅仅是 a、b、c……,当像 1、2、3……这样的任何数字,或像 !、@、、$……这样的任何特殊字符保存在单引号内时,它们将被视为字符字面量,可以分配给字符类型的变量,因此以下是一个有效的语句 −

Here, ch is a variable of character type which can hold a character of the implementation’s character set and 'a' is called a character literal or a character constant. Not only a, b, c,…​. but when any number like 1, 2, 3…​. or any special character like !, @, #, #, $,…​. is kept inside single quotes, then they will be treated as a character literal and can be assigned to a variable of character type, so the following is a valid statement −

char ch = '1';

字符数据类型消耗 8 位内存,这意味着您可以将 ASCII 值介于 -127 至 127 之间的内容存储在字符中,因此它可以容纳 256 个不同值中的任何一个。字符数据类型可以存储键盘上的任何字符,包括特殊字符,如 !、@、、$、%、^、&、*、(、)、_、+、{、} 等。

A character data type consumes 8 bits of memory which means you can store anything in a character whose ASCII value lies in between -127 to 127, so it can hold any of the 256 different values. A character data type can store any of the characters available on your keyboard including special characters like !, @, #, #, $, %, ^, &, *, (, ), _, +, {, }, etc.

请注意,您只能在单引号内保留一个字母或一个数字,且不允许在单引号内有多个字母或数字。因此,以下语句在 C 编程中无效 −

Note that you can keep only a single alphabet or a single digit number inside single quotes and more than one alphabets or digits are not allowed inside single quotes. So the following statements are invalid in C programming −

char ch1 = 'ab';
char ch2 = '10';

下面是一个简单的示例,展示如何在 C 编程语言中定义、分配和打印字符 −

Given below is a simple example, which shows how to define, assign, and print characters in C Programming language −

#include <stdio.h>

int main() {
   char  ch1;
   char  ch2;
   char  ch3;
   char  ch4;

   ch1 = 'a';
   ch2 = '1';
   ch3 = '$';
   ch4 = '+';

   printf( "ch1: %c\n", ch1);
   printf( "ch2: %c\n", ch2);
   printf( "ch3: %c\n", ch3);
   printf( "ch4: %c\n", ch4);
}

在此,我们使用 %c 来打印字符数据类型。当执行上述程序时,它将产生以下结果 −

Here, we used %c to print a character data type. When the above program is executed, it produces the following result −

ch1: a
ch2: 1
ch3: $
ch4: +

Escape Sequences

许多编程语言支持一个称为 Escape Sequence 的概念。当一个字符前带有反斜杠 (\),它称为转义序列,并且它对编译器具有特殊含义。例如,以下语句中的 \n 是一个有效字符,并且称为换行符 −

Many programming languages support a concept called Escape Sequence. When a character is preceded by a backslash (\), it is called an escape sequence and it has a special meaning to the compiler. For example, \n in the following statement is a valid character and it is called a new line character −

char ch = '\n';

在此,字符 n 前面带有一个反斜杠 (\),它具有特殊含义,即换行符,但请记住反斜杠 (\) 仅对几个字符具有特殊含义。以下语句在 C 编程中没有任何意义,并且将被认为是一个无效的语句 −

Here, character n has been preceded by a backslash (\), it has special meaning which is a new line but keep in mind that backslash (\) has special meaning with a few characters only. The following statement will not convey any meaning in C programming and it will be assumed as an invalid statement −

char ch = '\1';

下表列出了 C 编程语言中可用的转义序列 −

The following table lists the escape sequences available in C programming language −

Escape Sequence

Description

\t

Inserts a tab in the text at this point.

\b

Inserts a backspace in the text at this point.

\n

Inserts a newline in the text at this point.

\r

Inserts a carriage return in the text at this point.

\f

Inserts a form feed in the text at this point.

\'

Inserts a single quote character in the text at this point.

\"

Inserts a double quote character in the text at this point.

以下示例演示了编译器如何在 print 语句中解释转义序列 −

The following example shows how the compiler interprets an escape sequence in a print statement −

#include <stdio.h>

int main() {
   char  ch1;
   char  ch2;
   char  ch3;
   char  ch4;

   ch1 = '\t';
   ch2 = '\n';

   printf( "Test for tabspace %c and a newline %c will start here", ch1, ch2);
}

当执行上述程序时,它将生成以下结果 −

When the above program is executed, it produces the following result −

Test for tabspace     and a newline
will start here

Characters in Java

以下是用 Java 编写的等效程序。Java 处理字符数据类型的方式与我们在 C 编程中看到的很相似。但是,Java 为字符处理提供了额外的支持。

Following is the equivalent program written in Java. Java handles character data types much in the same way as we have seen in C programming. However, Java provides additional support for character manipulation.

您可以尝试执行以下程序以查看输出,其必须与上述 C 示例生成的输出相同。

You can try to execute the following program to see the output, which must be identical to the result generated by the above C example.

public class DemoJava {
   public static void main(String []args) {
      char  ch1;
      char  ch2;
      char  ch3;
      char  ch4;

      ch1 = 'a';
      ch2 = '1';
      ch3 = '$';
      ch4 = '+';

      System.out.format( "ch1: %c\n", ch1);
      System.out.format( "ch2: %c\n", ch2);
      System.out.format( "ch3: %c\n", ch3);
      System.out.format( "ch4: %c\n", ch4);
   }
}

当执行上述程序时,它将生成以下结果 −

When the above program is executed, it produces the following result −

ch1:  a
ch2:  1
ch3:  $
ch4:  +

Java 也以你在 C 编程中使用它们的方式支持转义序列。

Java also supports escape sequence in the same way you have used them in C programming.

Characters in Python

Python 不支持任何字符数据类型,但所有字符都被视为字符串,它是字符序列。我们将在单独的章节中学习字符串。在 Python 中使用单个字符时,您不需要做出任何特殊安排。

Python does not support any character data type but all the characters are treated as string, which is a sequence of characters. We will study strings in a separate chapter. You do not need to have any special arrangement while using a single character in Python.

以下是使用 Python 编写的等效程序−

Following is the equivalent program written in Python −

ch1 = 'a';
ch2 = '1';
ch3 = '$';
ch4 = '+';

print "ch1: ", ch1
print "ch2: ", ch2
print "ch3: ", ch3
print "ch4: ", ch4

当执行上述程序时,它将生成以下结果 −

When the above program is executed, it produces the following result −

ch1:  a
ch2:  1
ch3:  $
ch4:  +

Python 以你在 C 编程中使用它们的方式支持转义序列。

Python supports escape sequences in the same way as you have used them in C programming.