Lesson 6 - Understanding char Data Type
Learn how char stores single characters like letters and symbols, and how it differs from byte in beginner Arduino/ESP32 programs.
Progress indicator
Lesson 6 of 6
Learning Objectives
- Understand that char stores a single character such as a letter, digit, or symbol.
- Learn char syntax, declaration, initialization, range, and memory size basics.
- Compare char with byte and understand signed vs unsigned char in beginner-friendly language.
- Use char values to represent simple states or labels in Arduino/ESP32 code.
- Avoid common mistakes when working with quotes and single-character values.
- Explain the difference between a character like '7' and a number like 7.
Concept Explanation
What is char
char is a data type used to store a single character, such as a letter ('A'), a digit character ('7'), or a symbol ('#').
In Arduino and ESP32 code, char is often used for simple labels, command letters, menu choices, and small status markers.
Think of char as a tiny box for one symbol only. If you need a full word like "START", then you need a string (text), not a single char.
char Syntax
Basic syntax:
char variableName;You can also declare and initialize the value in one line using single quotes.
char letter;
char grade = 'A';
char symbol = '#';Important: 'A' is a single character (char), but "A" is a text string (even though it looks short).
Declaring char Variables
Declaring means creating a variable name and choosing char as the type so it can store one character value.
char command;
char ledStateChar;Initializing char
Initializing means giving a starting value. A char value is usually written inside single quotes like 'A'.
char grade = 'A';
char mode = 'M';Use single quotes for one character. Double quotes are used for strings (text with multiple characters).
Beginner tip
A digit character like '5' is not the same as the number 5. The first is a symbol used in text, the second is a numeric value used in math.
char Range
char stores a small numeric value internally, which can also represent a character. The exact numeric range can depend on whetherchar is treated as signed or unsigned on the platform.
Beginner rule: think of char mainly as a single character container unless you are learning ASCII numbers in more depth.
char Memory Size
A char usually uses 1 byte of memory. That is one reason it is useful for compact single-character storage.
When you need only one symbol, using char is simpler and lighter than using a full string.
Real-Life Example
Imagine a machine display that shows one mode letter: 'A' for Auto, 'M' for Manual. A single char is enough to store that mode marker.
char vs byte (Comparison)
| Feature | char | byte |
|---|---|---|
| Main use | Single characters / labels | Raw small numeric values (0-255) |
| Memory size | Usually 1 byte | 1 byte |
| Readability | Good for 'A', 'B', '#' | Good for numeric bytes |
Use char when you want to represent a character meaningfully. Use byte when the value is mainly a small raw number.
Example for char
char command = 'R'; (R = Run)
Example for byte
byte brightness = 180; (numeric PWM value)
Signed vs Unsigned char
char may be signed or unsigned depending on platform/compiler behavior. You can also explicitly use signed char or unsigned char when numeric behavior matters.
signed char
Can represent negative and positive small numbers.
unsigned char
Represents only non-negative values and is often used for raw byte-style data.
If you are storing letters and symbols, you usually write char and focus on readability. If you are doing numeric operations on raw 1-byte values, explicitly using unsigned char or byte may be clearer.
Why char Stores Single Characters
A char stores one small value (usually 1 byte). Character encoding systems (like ASCII) map letters and symbols to numeric values, so one byte can represent one character.
That is why 'A' is stored as a numeric code internally, but we write it as a character in code for readability.
ASCII idea (beginner)
Computers often use character tables (like ASCII) where each character has a numeric code. For example, 'A' has a numeric code, but writing'A' in code is easier for humans to understand.
When to Use char
- Store a command letter like
'A','B', or'R' - Store a simple status marker like
'O'(ON) or'F'(OFF) - Store menu choices entered from Serial input
- Work with single symbols or characters in text processing
If you need words, messages, or multiple letters together, move to strings.char is best when exactly one character is enough.
Example Code
This example uses char variables to label and track a simple LED state marker.
char ledLabel = 'A';
char statusChar = 'O'; // O = ON, F = OFF
char newlineChar = '\n';
void setup() {
Serial.begin(115200);
pinMode(2, OUTPUT);
}
void loop() {
digitalWrite(2, HIGH);
statusChar = 'O';
Serial.println("LED ON");
delay(500);
digitalWrite(2, LOW);
statusChar = 'F';
Serial.println("LED OFF");
delay(500);
}What this example teaches
- How to declare and initialize char variables
- How to update a char variable during program execution
- How a char can be used as a readable state marker
Code Explanation (Step-by-Step)
- char ledLabel = 'A'; stores a single character label for the LED or channel.
- char statusChar = 'O'; stores a single-character status marker (O for ON, F for OFF).
- char newlineChar = '\n'; shows that char can also store special characters (escape characters).
- setup() starts Serial and configures the LED output pin.
- When the LED turns ON, statusChar is updated to 'O'.
- When the LED turns OFF, statusChar is updated to 'F'.
- The char variables are small and readable for simple status tracking.
- This pattern is useful when you want a compact readable state without using a full string.
Example Code Explanation (Line by Line)
char ledLabel = 'A';creates a single-character label. This is useful when you want a short readable tag.char statusChar = 'O';stores a one-letter status marker for ON.char newlineChar = '\\n';shows that special characters can also be stored in a char variable.Serial.begin(115200);starts the Serial Monitor connection.pinMode(2, OUTPUT);prepares GPIO 2 so the code can control the LED.statusChar = 'O';updates the status marker when the LED is ON.statusChar = 'F';updates the status marker when the LED is OFF.- The program uses simple chars as readable markers instead of longer words.
Real-life example
A device menu may store the current mode as one letter: 'S' for Settings, 'R' for Run, 'T' for Test.
Common Mistakes with char
- Using double quotes (
"A") instead of single quotes ('A') for a char. - Trying to store multiple characters like
'AB'in one char variable. - Confusing a digit character (
'7') with the number7. - Forgetting that char is stored as a small numeric code internally.
- Using char when you actually need a full word or message (use a string instead).
Best Practices for char
- Use
charfor one-character labels, commands, and markers. - Use single quotes for char values, like
'A'or'#'. - Use descriptive names like
commandCharorstatusChar. - Use comments when a character has special meaning (for example,
'O'= ON). - Pick char only when one character is enough; use strings for multi-character text.
Try it now
Open the simulator workspace and practice reading variable changes while stepping through LED examples.