Menu
Coddy logo textTech

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 icon

Challenge

Easy

Create a program that:

  1. Declares a character variable named symbol
  2. Assigns the character '@' to this variable
  3. 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;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals