Characters
Part of the Fundamentals section of Coddy's C journey — lesson 8 of 63.
In C, the char data type is used to store a single character. Characters in C are enclosed in single quotes.
Declare a character variable:
char letter;Assign a character to the variable:
letter = 'A';You can also declare and initialize in one line:
char grade = 'B';A character occupies 1 byte of memory and is actually stored as an integer representing its ASCII value. For example, 'A' is stored as 65, 'B' as 66, and so on.
You can print a character using the %c format specifier:
printf("The letter is: %c\n", letter);Challenge
EasyCreate a program that:
- Declares a character variable named
symbol - Assigns the character '@' to this variable
- Prints the message "The symbol is: @" where @ is the value of your variable
Cheat sheet
The char data type stores a single character enclosed in single quotes:
char letter = 'A';Characters are stored as ASCII values (1 byte). Print using %c format specifier:
printf("The letter is: %c\n", letter);Try it yourself
#include <stdio.h>
int main() {
// Write your code here
return 0;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
3Operators
Arithmetic OperatorsModulo OperatorIncrement/DecrementAssignment OperatorsRelational OperatorsLogical Operators Part 1Logical Operators Part 2Logical Operators Part 3Recap Challenge