C++ char Data Type

Declaring and displaying char type variables in C++

#include <iostream>
using namespace std;

int main() {

char ch1 = 'a';
char ch2 = 'Z';
char ch3 = '7';

cout <<"First char variable value = " <<ch1 <<"\n";
cout <<"Second char variable value = " <<ch2 <<"\n";
cout <<"Third char variable value = " <<ch3 <<"\n";

  return 0;

}

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:

#include <iostream>
using namespace std;

int main() {

//Declaring char variables with ASCII values
char ch1 = 65;
char ch2 = 99;
char ch3 = 105;
char ch4 = 37;

//Display values
cout <<"ASCII 65 = " <<ch1 <<"\n";
cout <<"ASCII 99 = " <<ch2 <<"\n";
cout <<"ASCII 105 = " <<ch3 <<"\n";
cout <<"ASCII 37 = " <<ch4 <<"\n";

  return 0;

}

 

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:

#include <iostream>
using namespace std;

int main() {
char ch1 = 'F';

cout << "Size of char variable: " << sizeof(ch1) << endl;
cout << "Value of char variable: " << ch1 << endl;

  return 0;

}

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:

#include <limits>
#include <iostream>

int main(int argc, char* argv[])
{
std::cout << "Max char limit: "

           << static_cast<int>(std::numeric_limits<char>::max()) << "\n";

std::cout << "Max unsigned char: "

           << static_cast<int>(std::numeric_limits<unsigned char>::max()) << "\n";

}

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:

#include <iostream>
using namespace std;

int main() {
     char ch;

    //User input to check ASCII code for

                cout << "Enter a Character to Get ASCII code: ";

                //Assigning entered value to char type
                cin >> ch;

                //Using int function to get the ASCII code of char type
                cout <<"The ASCII code of "<< ch << " is "<< int(ch) << '\n';

                return 0;

}

Sample Output 1:

CPP-char-get-ASCII

Sample Output 1:

CPP-char-get-ASCII

Sample Output 1:

CPP-char-get-ASCII

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!