Skip to content

Trivia impact: rare

The Character With Size One

a quoted character that is not an int in C++

std::cout << sizeof('C') << '\n';

Does C++ give a character literal the same type as C does?

Answer
1

An ordinary character literal has type char, and sizeof(char) is always one.

Why

In C++, [lex.ccon] gives an ordinary character literal such as 'C' the type char. That differs from C, where the corresponding literal has type int. [expr.sizeof] defines sizeof(char) as one, regardless of how many bits a byte has on the machine, so the observed result is guaranteed rather than a GCC detail.

Where it shows up

It mostly surprises people translating C macros or using a character literal in a compile-time overload or type-trait test. Normal character arithmetic still promotes the char to an integer, which can hide the distinction in everyday expressions.

Takeaway: In C++, 'x' is a char; in C, it is an int.

Try it: g++ -std=c++17 main.cpp -o demo && ./demo

Open in Compiler Explorer ↗ Quiz this entry