Learn C - C Character Type






Values of type char occupy the least amount of memory of all the data types.

They typically require just one byte.

You can specify the initial value for a variable of type char by a character constant.

A character constant can be just a character written between single quotes. Here are some examples:

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

You can use an escape sequence between a pair of single quotes to specify a character constant, too:

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

You can also initialize a variable of type char with an integer value, as long as the value fits into the range for type char with your compiler, as in this example:

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

A variable of type char has a sort of dual personality: you can interpret it as a character or as an integer.

Here's an example of an arithmetic operation with a value of type char:

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

Thus, you can perform arithmetic on a value of type char and still treat it as a character.





Character Input and Character Output

You can read a single character from the keyboard and store it in a variable of type char using the scanf() function with the format specifier %c, for example:

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

To write a single character to the command line with the printf() function, you use the same format specifier, %c:

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

Of course, you can output the numeric value of a character, too:

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

This statement will output the value in ch as a character and as a numeric value.


    #include <stdio.h> 
      /*from w ww.ja v a 2 s .  c  o m*/
    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; 
    } 

The code above generates the following result.





Note

You can also output the integer values of the variables of type char as hexadecimal values by using the format specifier %x instead of %d.

Let's look at another example in which you apply arithmetic operations to values of type char:


    #include <stdio.h> 
      /*from  ww  w  .j  ava 2  s. c o  m*/
    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; 
    } 

The code above generates the following result.

Example

alters input, preserving non-letters


#include <stdio.h>
#include <ctype.h>            // for isalpha()
int main(void)
{//from   w  ww .  j  a va  2  s . c  o m
    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;
}

The code above generates the following result.

Example 2

alters input, preserving spaces


#include <stdio.h>
#define SPACE ' '             // that's quote-space-quote 
int main(void)
{//  w  w  w .  ja va 2s  .  c om
    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;
}

The code above generates the following result.

Example 3

The following code displays code number for a character.


#include <stdio.h>
int main(void)
{/*from   w  w  w  .  j ava  2 s . c om*/
    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;
}

The code above generates the following result.

Example 4

The following code uses escape characters.


#include <stdio.h>
int main(void)
{/*from   w  ww .j  a v  a  2  s  . co m*/
    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;
}

The code above generates the following result.