Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

char — fundamental character type

char is an integral character type occupying exactly one C++ byte. It is distinct from signed char and unsigned char, and its signedness is implementation-defined. A single char value does not necessarily represent a complete text character.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

char name{'A'};

char is a fundamental integral type used to store code units of ordinary text. The declaration char letter{'A'}; creates one object; the ordinary character literal 'A' has type char. Do not confuse it with "A", which is an array-typed string literal containing a terminating \0.

char, signed char, and unsigned char are three distinct types. The implementation determines whether plain char has the range and representation of signed char or unsigned char. Therefore, do not assume it can store negative values. When signedness matters, explicitly choose the appropriate type.

sizeof(char) is always 1: this means one C++ byte, not necessarily eight bits. A byte has at least eight bits; CHAR_BIT from <climits> gives the number. The numeric value of 'A' depends on the encoding—do not assume it is 65. Nor does char guarantee storage of a complete Unicode character; text may require multiple code units.

In the example, letter is printed as a character. The result of sizeof(letter) has value 1 and type std::size_t. In the verified environment, std::cout prints that value numerically. If std::size_t is an alias for unsigned char, however, std::cout prints the value as a character rather than the digit 1. zero contains the null character \0; static_cast<int>(zero) lets its numeric value 0 be printed. The example targets compilation in C++23 mode. The supplied site hosts an evolving working draft, not a frozen C++23 edition; it does not attest to the repository's production runtime.

Examples

#include <iostream>

int main() {
    char letter{'A'};
    char zero{'\0'};

    static_assert(sizeof(char) == 1);
    std::cout << letter << '\n';
    std::cout << sizeof(letter) << '\n';
    std::cout << static_cast<int>(zero) << '\n';
}

Result

A
1
0

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources