Lesson 22: Characters (char)

Learn the char type: 'A' versus "A", ASCII codes, integer representation, character input with scanf, and leftover newlines.

🎬
Watch this lesson (about 5 minutes; Japanese audio)
Slides and narration
📖 What you will learn
✅ Must-know
  • charis one character stored in a one-byte box
  • Write one character as 'A'(single quotes)
  • printf("%c", c)for output / scanf("%c", &c)for input
  • For consecutive input, read %c twice: discard the newline first
  • char stores an integer (an ASCII code)
⭐ Further reading
  • Convert uppercase to lowercase: c + ('a'-'A')
  • Convert a digit character to an integer: ch - '0'
  • A Japanese character does not fit in one char

Meet char: a box for one character

You have already used int for integers and double for real numbers. Now meet char, named after character. A char variable stores one character value and occupies one byte. The syntax is simple:
char c = 'A';          // Store the character A in c 
printf("%c\n", c);    // → A (display A on screen) 

🎨 Visual explanation: char is a single-cell box

char c =
'A'
one byte
→
printf("%c", c)
Screen output
→
A
Screen

⚠️ 'A'(single quotes) and "A"(double quotes) are different

Even with the same letter A, the quotes change the type and meaning . Keep this distinction in mind.
🔵 'A'— single quotes
A
one character (value stored in char: one byte; the literal itself has type int)
char c = 'A';
🟠 "A"— double quotes
A
\0
string (char array / ends with \0)
char s[] = "A";
The next lesson covers string : as shown on the right, "A" a string is an array of char values followed by \0 as its terminator .

💡 A char contains a number: a closer look

You now know the basics. Read on to understand why arithmetic works on char values .
In fact, a char stores a one-byte integer ; the screen displays 'A' because it interprets the number 65 as a character .
Useful to remember:
- char c = 'A';and char c = 65;is mean the same thing
- printf("%c", 65) → A / printf("%d", 'A') → 65
; this allows 'A' + 1 to become 'B' through character arithmetic

ASCII quick reference

Each character has a numeric code. The three key groups are digits (0–9) - uppercase letters (A–Z) - lowercase letters (a–z) .

① Common control characters and spaces

Character Decimal Hexadecimal Description
\000x00Null character (string terminator)
\t90x09Horizontal tab
\n100x0ANewline
␣320x20Space

② Digits (10)

Character 0123456789
Decimal 48495051525354555657

③ Uppercase letters (26)

Character ABCDEFGHIJKLM
Decimal 65666768697071727374757677
Character NOPQRSTUVWXYZ
Decimal 78798081828384858687888990

④ Lowercase letters (26)

Character abcdefghijklm
Decimal 979899100101102103104105106107108109
Character nopqrstuvwxyz
Decimal 110111112113114115116117118119120121122
👀 Notice: Uppercase A=65, lowercase a=97. 'a' - 'A' = 32 holds for these ASCII letters. This offset enables case conversion.

Example: characters and numbers are interchangeable

#include <stdio.h>

int main(void) {
    char c = 'A';
    printf("c = %c\n", c);     // → A (display as a character) 
    printf("c = %d\n", c);     // → 65 (display as an integer) 

    // Character arithmetic also works 
    printf("%c\n", c + 1);    // → B (65+1=66, the code for 'B') 
    printf("%c\n", 'Z' - 3);  // → W

    // Storing 65 in a char is equivalent to storing 'A' 
    char d = 65;
    printf("%c\n", d);        // → A
    return 0;
}

Common techniques

① Uppercase to lowercase
char c = 'A';
char lower = c + ('a' - 'A');   // 'A' + 32 = 'a'
// Or: char lower = c + 32; 
② Digit character to integer
char ch = '7';            // This is 55 (ASCII code for '7') 
int n = ch - '0';        // 55 - 48 = 7 (the integer seven) 
printf("%d\n", n);       // → 7
③ Test whether a character is uppercase or a digit
if (c >= 'A' && c <= 'Z') // Uppercase? 
if (c >= '0' && c <= '9') // Digit? 
// <ctype.h> also provides isupper(c) and isdigit(c) 

Why are characters represented as integers?

ASCII (American Standard Code for Information Interchange), developed in the 1960s, assigns seven-bit numbers to letters, digits, and symbols. C represents characters as integers; on ASCII-compatible systems this mapping makes character arithmetic straightforward.
Note: one char cannot hold a Japanese character
char c = 'あ'; produces an error or warning on common implementations. A Japanese character takes multiple bytes (typically three in UTF-8), so use an array of char, use wchar_t, or handle the encoding explicitly. The examples here use ASCII letters, digits, and symbols that fit in a char.
💡 Remember: computers store numbers
As discussed in Lesson 1, computers work with zeros and ones (electrical states) .
. Even when the screen shows 'A'or 'あ' , memory stores numeric codes . A char being an integer is simply the result of assigning a number to a character .
This idea will also help you understand strings, files, communication, and images as sequences of bytes (numbers).

Read a char with scanf

Now that you know char, learn to read one character from the keyboard . The basic syntax is easy, but leftover newlines in consecutive input require care, as explained below.

Basic usage

The input counterpart of printf is scanf. Use %c for a character, and pass the variable’s address with &, just as with %d for int.
#include <stdio.h>

int main(void) {
    char c;
    printf("Enter one character: " );
    scanf("%c", &c);          // Remember the &! 
    printf("Character entered: %c\n" , c);
    return 0;
}
/* Example Enter one character: A (type A and press Enter) Character entered: A */ 
Remember: printf("%c", c) displays c, while scanf("%c", &c) reads into c. Input requires its address, &c.

⚠️ Caution: consecutive calls to scanf can read a leftover newline

There is one trap when you call scanf more than once . For example, reading a number followed by a character:
int  n;
char c;
scanf("%d", &n);  // Enter a number, then press Enter 
scanf("%c", &c);  // Try to read a character next: this does not work as intended! 
printf("n=%d c=[%c]\n", n, c);
Even if you intend to enter 5, press Enter, and then enter A, c receives the leftover '\n' before you can type A. Input passes through a buffer: a waiting area for characters.

🔍 Look inside the input buffer

When you type 5 followed by Enter, the buffer contains:
5
Digit
\n
Newline from Enter
① scanf("%d", &n)is takes only 5 and stores it in n . The newline \nis remains :
5
→ n=5
\n
Still there!
② The next scanf("%c", &c)is reads the first character in the buffer . That character is \n, so:
\n
Front of buffer
→
c = '\n'
Unexpected!
→ A has not yet been entered, but c already contains \n instead.
③ Read %c using two calls . Discard the leftover newline first, then read the intended character:
\n
Discard on first call
→
A
Read on second call

✅ Fix: %cread twice

After numeric input, a newline can remain in the input buffer. Use %c twice: store the first character in dummy to discard it, then read the character you want.
int  n;
char dummy;
char c;
scanf("%d", &n);
scanf("%c", &dummy); // Discard the leftover '\n' 
scanf("%c", &c);     // Read the next character entered 
printf("n=%d c=[%c]\n", n, c); // → n=5 c=[A], as expected! 
Remember: The first %c discards the newline; the second %c reads the intended character. .
%d reads the number but can leave Enter's newline behind. %c treats that newline as a character, which is why consecutive input needs care.

✅ An easier fix: put a space before %c

A space in the format string means "skip any whitespace, tabs, and newlines". Writing " %c" skips the leftover newline and then reads one character, so you don't need dummy. This is what real programs usually use.
scanf("%d", &n);
scanf(" %c", &c);    // the space before %c skips the '\n'
* You can't use this when you want to read a space itself as the character. Also, the dummy approach assumes exactly one newline is left over, so it breaks if you type extra spaces before pressing Enter after "5".

Related lessons

Getting started
Lesson 3: Variables
Types such as int, double, and char: variables as boxes for values.
Loops, Arrays, and Strings
Lesson 23: Strings
Strings are char arrays ending in \0. Includes strlen and strcpy.
Getting started
Lesson 4: printf and scanf
Input, output, and C format specifiers.
Practice problems for this lessonStrings: 13 practice problemsWrite code in your browser and get it checked automatically. Hints and solutions included →
← Previous lesson
Lesson 21: Quiz (Arrays)
Next lesson →
Lesson 23: Strings

Review Quiz

Check your understanding of this lesson!

Q1. char c = 'A';, what does printf("%d", c) output?

A
65
Error

A char stores an integer (an ASCII code). 'A' is 65, so %d prints 65, while %c prints A.

Q2. 'A'(single quotes) and "A"(double quotes): what is the difference?

Both are the same single character
"A" is a string ending in \0 (two bytes)
'A' also has a trailing \0
In C, the character constant 'A' has type int. Storing its value in char c uses a one-byte variable. The string literal "A" is a two-byte char array containing 'A' and '\0'.

Q3. After the preceding scanf("%d", &n), how can you read one character correctly?

scanf("%c", &c)
scanf("%c", &dummy); scanf("%c", &c)
scanf("c", &c)

The first %c discards the leftover newline into dummy; the second reads the intended character. Although it's not among the choices, writing scanf(" %c", &c) (a space before %c) also skips the newline.

Share this lesson
Share on X (Twitter) Share on Facebook Send via LINE Hatena Bookmark