C++ Character Data Type


C++ Character (char) Data Type



”;


The character (char) data type in C++ stands for alphanumeric values, which can be a wide range of characters. These may include alphabets like ”a”, ”b”, and ”c”, numeric values like ”1”, ”2”, and ”3”, symbols like ”#”, ”$”, and ”&”, and many more.

The character data type takes 1 Byte (i.e., 8 bits) of memory space to store characters. In C++, the keyword “char” is used to declare a character variable.

In this tutorial, we will explore more about character data type, and its corresponding variables.

Use Character (char) Data Type

The following are some of the uses of character (char) data type −

  • The char data type is used when we need to hold only a single character and do not need the overhead of String.
  • The char data type can also be used in primitive form as an array without the use of a string literal.
  • In ASCII form, char data type can be used to represent numeric values and vice-versa.

Values of char Data Type

The character (char) data type in C++ can have multiple values, and these values are as follows −

  • Uppercase Alphabets, like A, B, Z, etc.
  • Lowercase Alphabets, like a, b, z, etc.
  • Symbols, like $, %, &, etc.
  • Escape Sequences, which will be discussed later in this article.

Creating a Character (char) Variable

We can declare a character variable using the “char” keyword followed by the variable name.

Syntax

Use the following syntax to create a char type variable −


char variable_name = [value];

Here, [value] is an optional and can be used to assign value during the declaration.

Example

In the following example, we are declaring a char variable, assigning a value to it.


// C++ program to demonstrate
// Character data type
#include <iostream> 
using namespace std;

int main(){ 
   char ch;
   return 0; 
}

Example of Character (char) Data Type

The following example shows the use of different character data types −


// C++ program to demonstrate
// Character data type
#include <iostream>
using namespace std;

int main() { 
   char ch,ch1,ch2;
   ch=''a'';     //this is an alphabet
   ch1=''&'';    //this is a symbol
   ch2=''1'';   //this is a number
   cout<<ch<<endl<<ch1<<endl<<ch2<<endl;    
   return 0; 
}

Output


a
&
1

ASCII Values of Characters

ASCII stands for the “American Standard Code for Information Interchange”. It was the first set of encoding values assigned to different characters and symbols. The character sets used in modern computers, in HTML, and on the Internet, are all based on ASCII.

The ASCII table describes a numeric value for all character types, and these values can be used to declare a character without the explicit use of the character itself. It contains the numbers from 0-9, the upper and lower case English letters from A to Z, and some special characters.

The following data gives a reference for all ASCII values of characters available in C++ −


ASCII Range of ''a'' to ''z'' =  97-122
ASCII Range of ''A'' to ''Z'' =  65-90
ASCII Range of ''0'' to ''9'' = 48-57

Example to Show ASCII Declaration

The following example shows how a user can declare a character variable using ASCII values, without explicit usage of the character itself −


#include <iostream>
using namespace std; 

int main() { 
   char ch,ch1,ch2;
   ch=65;     //this is an alphabet
   ch1=45;    //this is a symbol
   ch2=55;   //this is a number
   cout<<ch<<endl<<ch1<<endl<<ch2<<endl;    

   return 0; 
}

Output


A
-
7

Implicit Conversion of Character Variables

Character variables can be implicitly converted to their integer values using ASCII references, and vice-versa. Hence, when we declare a character in C++, we can reference their ASCII value, whereas an ASCII numerical can be used to access its character value as well. This is done using implicit conversion or typecasting of data types.

We can add a keyword of the data type we need to convert the given variable into, and the compiler changes the data type automatically. For example, if we write char(97), it will load the character value of ASCII number 97, which is ‘a’. This is also possible for converting the character data type to integral (ASCII) values.

This is clearly explained in the examples given below −

Example

The following example shows the implicit typecasting of char to int, and vice-versa −


#include <iostream>
using namespace std;

int main() {
   char c = ''$'';
   int a = 97;
   cout << "The Corresponding ASCII value of ''$'' : ";
   cout << int(c) << endl;

   cout << "The Corresponding  character value of 97 : ";
   cout << char(a) << endl;

   return 0;
}

Output


The Corresponding ASCII value of ''$'' : 36
The Corresponding  character value of 97 : a

ESCAPE SEQUENCE IN C++

Character variables which begin with a backslash (“”) are called escape sequences. These determine on the output sequence on the output window of a compiler. In this context, backslash is also called as the ‘Escape Character’.

The following table shows different types of escape sequences available in C++ −









S. No. Escape Sequences Character
1. n Newline
2. \ Backslash
3. t Horizontal Tab
4. v Vertical Tab
5. Null Character

The usage of escape sequences is clearly explained in the following example code −

Example 1


#include <iostream>
using namespace std;

int main() {
   char a = ''H'';
   char b = ''E'';
   char c = ''Y'';
   char d = ''n''; //enter new line
   char e = ''t''; //tab to enter space 
   cout << a << b << c << d << a << b << c << e << a << b << c;
   return 0;
}

Output


HEY	HEYHEY
HEY	HEY

The escape character can also be used to insert special characters into strings. This is clearly explained in the example given below −

Example 2


#include <iostream>
using namespace std;
 
int main() {
   //string txt = "Hey, where are you "vikas" ? ";
   //this throws error
   string txt = "Hey, where are you "vikas"? ";

   cout<<txt<<endl;
   return 0; 
}

Output


Hey, where are you "vikas"? 

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *