Char in C++
Char in C++
Char is a character data type in C and C++ that is used to represent single ASCII character. It is the building block for string which is basically just a list of characters. A char actually stores an integer value that coressponds to a ASCII code. Char as many types of chars like, char, signed char, unsigned char, char8_t, char16_t, and char32_t important ones are discussed in this post. Understanding char is a must if you want to have a very good understanding of strings and text processing.
| Type | Size | Range / Description |
|---|---|---|
char | 1 byte | Implementation-defined: • -128 to 127 (signed)• 0 to 255 (unsigned) |
signed char | 1 byte | -128 to 127 |
unsigned char | 1 byte | 0 to 255 |
wchar_t | 2–4 bytes | Depends on implementation |
char8_t | 1 byte (8-bit) | UTF-8 encoding |
char16_t | 2 bytes (16-bit) | UTF-16 encoding |
char32_t | 4 bytes (32-bit) | UTF-32 encoding |
char data type
char data type is a byte sized (typically 8-bit but not always) integral data type that is used to store characters in ASCII format. It is different from both signed and unsigned chars, so user should not make any assumption that it is either signed or unsigned. Depending on if it is signed or unsigned, it can store values from and up to -127 to 127 and 0 to 255 respectively. When we assign a character literal to a variable, the compiler does a ASCII table lookup and replace character that character with its equivalent ASCII integer value.

char a = 'A';
cout << (int)a << endl; //65
When we cast the char data type to the int it does an ASCII table lookup and check for the equivalent integer value for the character ‘A’, in this examplem, which is 65, and it prints that on the console.
One very interesting thing you could try is adding some number to a char what will it print? Lets take a look.
char val = 'a';
char val2 = val + 1; // b
char val3 = val + 25; // z
In the avove example when we initialize a variable val whose value is initialized to ‘a’ , Then in next line, 1 is added to val which adds this to its character’s ASCII, which increments ‘a’ from 97(a’s ASCII code) to 98 (b’s ASCII code) and assigs it to the variable val2. Finally, the val is incremented by 25 incrementing the ASCII code from 97 to 122(z’s ASCII code). If we print and check the given variables, we will get the results, a, b and z respectively.
signed & unsigned char
The signed and unsigned char are very similar to the char type but the signed version can store both positive and negative integers, while the unsigned version can store only positive numbers. The size of both these version of chars are 1-byte.
signed char
The signed char is a byte size char that can also store negative integers. Its value ranges from -128 to 127. The main purpose of a signed char is not to store characters, but it acts like a byte size integer, imagine it like int8, for storing small int values.
signed char signal_data=11;
unsigned char
The unsigned char is also a byte size integral data type that can store only positive values ranging from 0 to 255. This data type is very useful when we want to create buffers in memory, handle binary data or to handle file bytes.
unsigned char strbuf{};
char array and std::string
Strings in C++ are just array of characters terminated by the ‘\0’ also called the null terminator, it is automatically handled if you use std::string instead of raw char array which have to be manually managed.
Char Array Syntax
char arr[] = "Angel";
std::string syntax
std::string name = "Angel";
Useful Library Functions for Chars
The
| Function | Description |
|---|---|
std::isalpha(c) | Checks if the argument is an alphabetic character. |
std::isdigit(c) | Checks if the argument is a digit. |
std::isalnum(c) | Checks if the argument is alphanumeric. |
std::isspace(c) | Checks if the argument is a whitespace character. |
std::toupper(c) | Converts the argument to uppercase. |
std::tolower(c) | Converts the argument to lowercase. |
References
-
Marc Gregoire, Professional C++, Fifth Edition (ISBN: 978-1-119-69540-0)
-
Stack Overflow – Under what circumstances would one use a signed char in C++? – Filipe Gonçalves
-
Stack Overflow – Will a
charalways-always-always have 8 bits? – Carl Norum -
Cppreference.com – Standard library header
<cctype> -
Cppreference.com – ASCII Image Screenshot