Type Difference of Character Literals in C and C++



Character literals are those values, which are assigned to the variable of character data type. It is written as a single character enclosed in single quotes (' ') like 'A', 'b' or '2'.

But if we see how these character literals are stored in memory, their type differs. In C the character literals are stored as type int, whereas the same character literal is stored as type char in C++. In this article, we will study about the differences between these two in detail.

Type of character literal in C

The type of character literals in C language is an integer (int) as it occupies 4 bytes memory. This happens because each individual character is associated with a unique ASCII value, and when we store that character, it is internally converted to its corresponding ASCII value.

Example

Here is the following example showing how a character is stored in integer form by printing its ASCII value using format specifier %d (which is used to print signed integers):

#include <stdio.h>

int main() {
    printf("%c\n", 'A');  // Output: A (the character itself)
    printf("%d\n", 'A');  // Output: 65 (ASCII value of 'A')
    
    // still valid as 'B' is of type int in C therefore will return no error
    int x = 'B';  
    return 0;
}

Output

A
65

Type of character literal in C++

In C++, character literals are treated as a char only and occupies 1 byte of memory, because C++ supports stronger type-checking compared to C.

Example

Here is the following example code printing the character value and its ASCII value by using static_cast<int>(x), which is used to explicitly convert the char to int to display the ASCII value:

#include <iostream>
using namespace std;

int main() {

    char x = 'A';  // char literal A stored in character data type variable x
    cout << "Character: " << x << endl;
    
    // printing its ASCII value by using static_cast<int> which will explicitly convert the char to int
    cout << "ASCII value: " << static_cast<int>(x) << endl;

    return 0;
}

Output

Character: A
ASCII value: 65
Akansha Kumari
Akansha Kumari

Hi, I am Akansha, a Technical Content Engineer with a passion for simplifying complex tech concepts.

Updated on: 2025-06-10T17:17:06+05:30

651 Views

Kickstart Your Career

Get certified by completing the course

Get Started