C 字符类型

2018-05-20 15:00 更新

学习C - C字符类型

char类型的值占用所有数据类型的最小内存量。

它们通常只需要一个字节。

您可以通过字符常量为char类型的变量指定初始值。

字符常数可以只是单引号之间的字符。这里有些例子:

char letter = "A"; 
char digit = "9"; 
char exclamation = "!"; 

您可以使用一对单引号之间的转义序列来指定字符常量:

char newline = "\n"; 
char tab = "\t"; 
char single_quote = "\""; 

您也可以使用整数值初始化char类型的变量,只要该值适用于您的编译器的char类型的范围,如下例所示:

char character = 74;                        // ASCII code for the letter J

char类型的变量具有一种双重个性:您可以将其解释为字符或整数。

下面是一个值为char类型的算术运算的例子:

  
char letter = "C";  // letter contains the decimal code value 67 
letter = letter + 3;// letter now contains 70, which is "F" 

因此,您可以对char类型的值执行算术,并将其视为字符。


字符输入和字符输出

您可以从键盘读取单个字符,并使用格式说明符%c的scanf()函数将其存储在char类型的变量中,例如:

  
char ch = 0; 
scanf("%c", &ch);                           // Read one character 

要使用printf()函数将单个字符写入命令行,请使用相同的格式说明符%c :

printf("The character is %c\n", ch);

当然,也可以输出一个字符的数值:

printf("The character is %c and the code value is %d\n", ch, ch);

该语句将以ch作为字符和数值输出值。


    #include <stdio.h> 

    int main(void) 
    { 
      char first = "A"; 
      char second = 63; 
      
      printf("The first example as a letter looks like this - %c\n", first); 
      printf("The first example as a number looks like this - %d\n", first); 
      printf("The second example as a letter looks like this - %c\n", second); 
      printf("The second example as a number looks like this - %d\n", second); 
      return 0; 
    } 

上面的代码生成以下结果。


注意

您还可以使用格式说明符%x而不是%d将char类型的变量的整数值输出为十六进制值。

我们来看另外一个例子,你将算术运算应用到char类型的值中:


    #include <stdio.h> 

    int main(void) 
    { 
      char first = "A"; 
      char second = "B"; 
      char last = "Z"; 
      
      char number = 40; 
      
      char ex1 = first + 2;                     // Add 2 to "A" 
      char ex2 = second - 1;                    // Subtract 1 from "B" 
      char ex3 = last + 2;                      // Add 2 to "Z" 
      
      printf("Character values      %-5c%-5c%-5c\n", ex1, ex2, ex3); 
      printf("Numerical equivalents %-5d%-5d%-5d\n", ex1, ex2, ex3); 
      printf("The number %d is the code for the character %c\n", number, number); 
      return 0; 
    } 

上面的代码生成以下结果。

例子

改变输入,保留非字母


#include <stdio.h>
#include <ctype.h>            // for isalpha()
int main(void)
{
    char ch;
    
    while ((ch = getchar()) != "\n")
    {
        if (isalpha(ch))      // if a letter,
            putchar(ch + 1);  // display next letter
        else                  // otherwise,
            putchar(ch);      // display as is
    }
    putchar(ch);              // display the newline
    
    return 0;
}

上面的代码生成以下结果。

例2

改变输入,保留空格


#include <stdio.h>
#define SPACE " "             // that"s quote-space-quote 
int main(void)
{
    char ch;
    
    ch = getchar();           // read a character         
    while (ch != "\n")        // while not end of line    
    {
        if (ch == SPACE)      // leave the space          
            putchar(ch);      // character unchanged      
        else
            putchar(ch + 1);  // change other characters  
        ch = getchar();       // get next character       
    }
    putchar(ch);              // print the newline        
    
    return 0;
}

上面的代码生成以下结果。

例3

以下代码显示字符的代码。


#include <stdio.h>
int main(void)
{
    char ch;
    
    printf("Please enter a character.\n");
    scanf("%c", &ch);   /* user inputs character */
    printf("The code for %c is %d.\n", ch, ch);
    
    return 0;
}

上面的代码生成以下结果。

例4

以下代码使用转义字符。


#include <stdio.h>
int main(void)
{
    float salary;
    
    printf("\aEnter your desired monthly salary:");/* 1 */
    printf(" $_______\b\b\b\b\b\b\b");             /* 2 */
    scanf("%f", &salary);
    printf("\n\t$%.2f a month is $%.2f a year.", salary,
           salary * 12.0);                         /* 3 */
    printf("\rGee!\n");                            /* 4 */
    
    return 0;
}

上面的代码生成以下结果。

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号