C - Data Type char Type


char type is an unsigned type, and the value stored in a variable of type char can range from 0 to 255.

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 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:

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

A variable of type char can be integer and character at the same time.

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.

Escape Sequences

What It Represents or Displays
Bell ("beep!")
Backspace, non-erasing
Form feed or clear the screen
Carriage return
Vertical tab
Backslash character
Question mark
Single quote
Double quote
Hexadecimal character code nn
Octal character code nn
Octal character code nn

Related Topics