Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 135 · Variables and Types

Characters and ASCII

Learn how C stores characters as small integers, read the ASCII table, use escape sequences and ctype.h functions safely, and handle the signedness of char.

In C, a character is just a number. The type char is a one-byte integer, and the letter 'A' is simply a convenient way to write the number 65. When you print that number with %c, printf looks it up in the character set and shows a letter; print it with %d and you see the code. Understanding this dual nature is the key to working with text in C: converting case, checking digits, parsing input and building strings all come down to arithmetic on small integers.

This lesson covers how char works, the ASCII table, character literals and escape sequences, the classification functions in <ctype.h> (and the one rule that keeps them safe), the surprising fact that char may be signed or unsigned, and a first look at characters beyond ASCII.

A char is a small integer

A char occupies exactly one byte (sizeof(char) is always 1). It can hold a character code, and because it's an integer type you can do arithmetic and comparisons with it:

C Characters are numbers

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

digit - '0' is the classic way to turn a digit character into its value. It works because the C standard guarantees that '0' to '9' have consecutive codes, in every character set. (The letters aren't guaranteed to be consecutive in every character set, but in ASCII they are.)

The ASCII table

Almost every system today uses a character set whose first 128 codes are ASCII (the American Standard Code for Information Interchange). The layout has a logic worth knowing:

Codes Contents
0–31 Control characters: 0 is the null character '\0', 9 is tab, 10 is newline, 13 is carriage return
32 Space
48–57 Digits '0'–'9'
65–90 Uppercase 'A'–'Z'
97–122 Lowercase 'a'–'z'
33–47, 58–64, 91–96, 123–126 Punctuation and symbols
127 DEL (a control character)

Uppercase and lowercase letters are exactly 32 apart: 'a' is 97 and 'A' is 65. In binary, that's a single bit (value 32), so flipping the case of an ASCII letter is one XOR. You can print the whole printable range yourself:

C Print the printable ASCII table

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The loop counter is an int, and %c accepts an int, printing the character with that code. Code 32 shows as a blank because it's a space.

Character literals and escape sequences

A character literal is one character in single quotes: 'x', '7', ' '. Double quotes make a string, which is a different thing: "x" is an array of two bytes, 'x' and the terminating '\0'.

Some characters can't be typed directly between quotes, so C has escape sequences, written with a backslash:

Escape Meaning Code
'\n' Newline 10
'\t' Horizontal tab 9
'\r' Carriage return 13
'\0' Null character (ends every string) 0
'\\' Backslash 92
'\'' Single quote 39
'\"' Double quote (also fine unescaped in a char literal) 34
'\a' Alert (bell) 7
'\x41' Hexadecimal code: here 0x41, 'A' 65
'\101' Octal code (1–3 digits): here 'A' 65
C Escape sequences

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The last two lines reveal a C quirk: a character literal like 'A' has type int, not char, so sizeof 'A' is 4 on our server. In C++ it's a char (size 1). It rarely matters in practice, but it's a favorite interview question; C and C++ lists more differences like this.

Don't put two characters in single quotes. 'ab' is a multi-character constant whose value is implementation-defined. GCC accepts it with a -Wmultichar warning. It's almost always a typo for "ab".

Classifying and converting: ctype.h

<ctype.h> provides functions to test and convert characters. They work with any character set, handle locales, and are clearer than comparing ranges by hand:

Function True for
isalpha(c) Letters
isdigit(c) Decimal digits 0–9
isalnum(c) Letters or digits
isspace(c) Space, \t, \n, \v, \f, \r
isupper(c), islower(c) Uppercase / lowercase letters
ispunct(c) Printable characters that aren't letters, digits or space
isxdigit(c) Hex digits 0-9a-fA-F
isprint(c), iscntrl(c) Printable / control characters
toupper(c), tolower(c) Convert the case (other characters are returned unchanged)

The is functions return a non-zero int for "true" (not necessarily 1) and 0 for "false", so test them with if (isdigit(c)), never == 1.

C Counting character kinds

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Notice that c is an int, not a char. getchar returns every possible byte value (0–255) plus the special value EOF (usually −1), so it needs a type wider than char to keep them apart. Storing the result in a char is a classic bug, explained in Reading Input.

The unsigned char rule

The <ctype.h> functions take an int that must be either EOF or a value representable as an unsigned char (0–255). Passing a negative char value other than EOF is undefined behavior. That matters because on many platforms char is signed, and bytes above 127 (such as the UTF-8 bytes of é) become negative numbers. The safe pattern when you have a char (not a getchar result) is to cast it first:

C
char text[] = "Größe";
for (size_t i = 0; text[i] != '\0'; i++) {
    if (isalpha((unsigned char)text[i])) {   // the cast keeps the argument in range
        /* ... */
    }
}

Is char signed or unsigned?

C has three distinct character types: char, signed char and unsigned char. signed char holds −128 to 127, unsigned char holds 0 to 255, and plain char behaves like one of them, chosen by the platform. It's signed on x86 and x86-64, and unsigned on most ARM and PowerPC Linux systems. CHAR_MIN in <limits.h> tells you which:

C The signedness of char

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

On our x86-64 server the byte 0xE9 reads as −23 through a plain char, so the program takes the "negative" branch. On an ARM machine the same program would print 233 and take the other branch (and GCC would warn there that byte < 0 is always false). Rules of thumb:

  • Use char for text.
  • Use unsigned char (or uint8_t) for raw bytes: binary data, checksums, pixels.
  • Use signed char only when you really want a tiny signed number.
  • Cast to unsigned char before passing a char to <ctype.h> or using it as an array index.

Beyond ASCII: UTF-8

ASCII only covers English letters. Modern systems store other characters, like é, ж, 中 or emoji, in UTF-8, which encodes each one as a sequence of 1 to 4 bytes. ASCII characters stay single bytes, so all ASCII text is valid UTF-8. Every byte of a multi-byte character is 128 or above.

In C, a UTF-8 string is still an array of char, but one character may take several bytes:

C Characters versus bytes in UTF-8

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

strlen counts bytes, so "café" has a length of 5: é takes two bytes (0xC3 0xA9). Counting the bytes that don't start with the bit pattern 10 gives the number of characters. C11 added char16_t and char32_t with u"..." and U"..." literals, and C23 added char8_t with u8 character literals, but full Unicode processing (case mapping, normalization) needs a library such as ICU. For most C programs, treating UTF-8 text as bytes and leaving it intact is enough.

Check your understanding

Quick check · 5 questions

of 5 answered You scored / 5 · perfect!

  1. 1. What does printf("%d", 'C' - 'A'); print?

    Characters are integers. 'C' is 67 and 'A' is 65 in ASCII, so the difference is 2.
  2. 2. Why should the result of getchar() be stored in an int?

    getchar returns any byte 0-255 or EOF (a negative value). A char can't hold all 257 possibilities, so a real byte could be mistaken for EOF, or EOF missed.
  3. 3. What is sizeof 'x' in C on our server?

    In C, a character literal like 'x' has type int, so its size is sizeof(int), 4 here. In C++ it would be 1.
  4. 4. On x86-64 Linux, plain char c = (char)200; What does printf("%d", c) print?

    Plain char is signed on x86-64, so the byte 200 (0xC8) is read as 200 - 256 = -56. On a platform with unsigned char it would print 200.
  5. 5. How many bytes does strlen("naïve") count in a UTF-8 source file?

    The letters n, a, v and e are one byte each, and ï takes two bytes in UTF-8, so strlen returns 6 even though there are 5 characters.

Summary

  • A char is a one-byte integer; 'A' is just the number 65 in ASCII, and %c vs %d decides how it's printed.
  • Digits '0'–'9' are always consecutive, so c - '0' gives a digit's value; ASCII letters are consecutive too, and cases differ by 32.
  • Escape sequences like '\n', '\t', '\0', '\\' and '\x41' write special characters; character literals have type int in C.
  • <ctype.h> functions (isalpha, isdigit, toupper, …) need an unsigned char value or EOF: cast char arguments to unsigned char.
  • Plain char is signed on some platforms and unsigned on others; use unsigned char for raw bytes.
  • UTF-8 stores non-ASCII characters in several bytes, so strlen counts bytes, not characters.

Next: Booleans, where you'll learn how C represents true and false.