A girl and a robot thinking together

Think together · Start with a story

The robot needs to record the answer choice A and whether an answer is correct. One stores a symbol; the other stores true or false.

Break down the process

Follow the numbered steps, then explain them in your own words
  1. Choicechar stores A
  2. CompareIs the choice A?
  3. Resultbool stores true

char can store a basic unit of character encoding, while bool represents true or false.

In a beginner example, 'A' is a character and true means a condition is met. They are not the same kind of data.

01What is char?

char is one of C++'s character types.

char grade = 'A';

'A' inside single quotation marks is a character literal.

At a lower level, char stores an integer code value, which a program interprets as a character according to an encoding rule.

02How can a character become a number?

For example, in ASCII-compatible encodings, the code value for uppercase A is 65.

char c = 'A';
std::cout << static_cast<int>(c);

In common environments, this prints 65.

03Can one char store one Chinese character?

Usually, do not think of “one char = one Chinese character.”

char can hold at least one byte-sized addressable unit. A common Chinese character in UTF-8 usually needs several bytes.

04What is bool?

bool stores logical truth values:

bool passed = true;
bool gameOver = false;

true

Means logically “true.”

false

Means logically “false.”

05Does bool use only 1 bit in memory?

Not necessarily. Although there are only two logical states, C++ objects must also meet addressing and implementation requirements.

In common implementations, sizeof(bool) is often 1 byte, not 1 bit.

06What kind of types are char and bool?

Both are fundamental C++ types.

char

Useful for small integer codes and character code units.

bool

Useful for representing a true-or-false state.

What you have learned

  • char is a character type that fundamentally stores a small integer value.
  • Character encoding determines how a value corresponds to a character.
  • One char is not simply the same as “one Chinese character.”
  • bool has two logical values: true and false.
  • bool has two logical states, but its storage is not necessarily just 1 bit.

Next: What does it mean to assign a value to a variable?

Your turn to try

Is “was an umbrella brought?” better stored as a bool, or should we record the umbrella's weight as an integer?

Ready? Open to see the explanation

If you only care whether an umbrella was brought, bool is more direct. To record its weight, you need another piece of data. Think about the question first, then choose a type.