Hit enter after type your search item

C++ char Data Type

Declaring and displaying char type variables in C++


Output:

CPP-char

C++ char data type in short

  • The char is a data type in C++
  • It is used to store a single character
  • The character must be enclosed in a single quote e.g. ‘a’, ‘b’, ‘5’
  • The char type takes one byte (8 bits) in the memory
  • The value of char is stored as an integer in the memory
  • You may also assign ASCII values to the char type (See example in the section below)
  • Though it depends on the compiler, the maximum value that a char type can store is typically 255.

Assigning ASCII code value to char type

In the following example, we assigned ASCII values to the three char-type variables. Then we displayed their values:


 

Output:

CPP-char-ascii

You saw as we assign a value without a single quote, it is taken as an ASCII value. As we displayed the variable, it display the corresponding value to the ASCII character.

Checking memory size of char type

By using sizeof() function, you may check the size of a variable. The C++ program below displays the size of char type variables:


Output:

CPP-char-size

You saw the size is displayed as 1. That means, one byte of memory taken by the char type variable

Getting the highest value a char type can store

The example below shows the maximum value a char type can store:


Output:

CPP-char-max

Get the ASCII value of User entered character

In the following program, the user is asked to enter a character. We will then display the ASCII value of that character:


Sample Output 1:

CPP-char-get-ASCII

Sample Output 1:

CPP-char-get-ASCII

Sample Output 1:

CPP-char-get-ASCII

This div height required for enabling the sticky sidebar