C Character Variable Assignment Rules
In C language, character variables can be assigned a character directly or using ASCII code. For example:
char ch1 = 'A'; // 直接赋值一个字符'A'给变量ch1
char ch2 = 65; // 使用ASCII码赋值,ASCII码65代表字符'A'
In addition, special characters like escape characters (such as newline character ‘\n’ and tab character ‘\t’) can be assigned to character variables in the C language.
char newline = '\n'; // 赋值换行符给变量newline
char tab = '\t'; // 赋值制表符给变量tab
It should be noted that character variables can only store one character. Even if multiple characters are assigned, only the last character will be stored. For example:
char ch = 'AB'; // 错误示例:赋值了两个字符,只会存储最后一个字符'B'
In addition, character variables can be converted to integer variables, with the value being the corresponding ASCII code of the character. For example:
char ch = 'A';
int ascii = ch; // 将字符'A'的ASCII码赋值给整型变量ascii
It is important to note that character variables can also perform arithmetic operations, which essentially involve calculations on ASCII codes. For example:
char ch = 'A';
ch = ch + 1; // 将字符'A'的ASCII码加1,结果为字符'B'