Lesson 31 +10 XP

Characters

Characters

A char stores a single character: a letter, a digit, a symbol. It's small, simple, and sneakily clever under the hood.

char and single quotes

A char literal is written in single quotes:

char letter = 'A';
char digit = '7';
char symbol = '!';

A char is secretly a number

A char actually stores a whole number: the character's code. Most systems use ASCII, where 'A' is code 65, 'a' is 97, and '0' is 48.

char c = 'A';
printf("%c\n", c);       // prints A
printf("%d\n", c);       // prints 65, the ASCII code

Sizes

A char is 1 byte - the smallest addressable unit of memory.

ASCII is the star

ASCII maps characters to numbers:

CharCode
'A'65
'a'97
'0'48
' '32

TL;DR

  • char stores a single character, written in single quotes.
  • It's really a number: the ASCII code ('A' is 65).
  • A char is 1 byte.
  • ASCII maps each character to a numeric code.