C - Data Type char Type

Introduction

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

Characters
What It Represents or Displays
\a
Bell ("beep!")
\b
Backspace, non-erasing
\f
Form feed or clear the screen
\n
Newline
\r
Carriage return
\t
Tab
\v
Vertical tab
\\
Backslash character
\?
Question mark
\'
Single quote
\"
Double quote
\xnn
Hexadecimal character code nn
\onn
Octal character code nn
\nn
Octal character code nn

Related Topics

Exercise