Type difference of character literals in C and C++

Character literals are values assigned to character data type variables. They are written as single characters enclosed in single quotes (' ') like 'A', 'b' or '2'.

However, the type of character literals differs between C and C++. In C, character literals are stored as type int, whereas in C++ they are stored as type char. This fundamental difference affects memory usage and type safety.

Syntax

'character'  // Character literal syntax

Type of Character Literal in C

In C, character literals have type int and occupy 4 bytes of memory. This happens because each character is internally represented by its ASCII value, which is an integer.

Example

The following example demonstrates how character literals are treated as integers in C −

#include <stdio.h>

int main() {
    printf("Character: %c\n", 'A');  // Output: A (the character itself)
    printf("ASCII value: %d\n", 'A');  // Output: 65 (ASCII value of 'A')
    printf("Size of 'A': %zu bytes\n", sizeof('A'));
    
    // Valid assignment since 'B' is of type int in C
    int x = 'B';  
    printf("Variable x contains: %d\n", x);
    
    return 0;
}
Character: A
ASCII value: 65
Size of 'A': 4 bytes
Variable x contains: 66

Type of Character Literal in C++

In C++, character literals have type char and occupy 1 byte of memory. This provides stronger type safety and more efficient memory usage compared to C.

Example

Here's how character literals work in C++ (shown for comparison, but this article focuses on C) −

#include <iostream>
using namespace std;

int main() {
    char x = 'A';  
    cout << "Character: " << x << endl;
    cout << "ASCII value: " << static_cast<int>(x) << endl;
    cout << "Size of 'A': " << sizeof('A') << " byte" << endl;
    return 0;
}

Key Differences

Aspect C Language C++ Language
Type of character literal int char
Memory size 4 bytes 1 byte
Type checking Weaker Stronger

Conclusion

In C, character literals are treated as integers (4 bytes), while C++ treats them as char type (1 byte). Understanding this difference is important for memory optimization and type safety in C programming.

Akansha Kumari
Akansha Kumari

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

Updated on: 2026-03-15T10:13:39+05:30

781 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements