Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Char | Text Data Type
C++ Data Types
course content

Contenido del Curso

C++ Data Types

C++ Data Types

1. Introduction
2. Numerical Data Types
3. Text Data Type
4. Other Data Types and Concepts

bookChar

To store text data in C++, the char data type is used to store a single character, like 'A' or 'w'. In the upcoming chapter, we will explore the process of combining these individual characters into sequences to form words, sentences, and more. However, for now, let's focus on the char data type for single character storage.

cpp

main

copy
1234567
#include <iostream> int main() { char letter = 'G'; std::cout << letter << std::endl; }

Note

char's should be specified in single quotes. Even if the character you hold is a number, you should put it in single quotes, '9', not 9.

You can play with the code above to see what happens if you use double quotes or assign numbers without quotes.

The char data type and memory.

To be stored in memory, it is first converted to a number using ASCII table. The binary representation of that number is then stored in memory.
You can take a quick look at the ASCII table below (the first column is not valuable for us).

cpp

main

copy
12345678
#include <iostream> int main() { // change the number to output different symbol char symbol = 100; std::cout << symbol; }

Note

If you assign a number without single quotes to a char (for example, char letter = 76), the compiler assumes that you specified a character already converted to a number.

As you can see from the table, 76 corresponds to L, so the value of letter is 'L'.

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 1
some-alt