
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Hello World
- C++ Omitting Namespace
- C++ Tokens
- C++ Constants/Literals
- C++ Keywords
- C++ Identifiers
- C++ Data Types
- C++ Numeric Data Types
- C++ Character Data Type
- C++ Boolean Data Type
- C++ Variable Types
- C++ Variable Scope
- C++ Multiple Variables
- C++ Basic Input/Output
- C++ Modifier Types
- C++ Storage Classes
- C++ Numbers
- C++ Enumeration
- C++ Enum Class
- C++ References
- C++ Date & Time
- C++ Operators
- C++ Arithmetic Operators
- C++ Relational Operators
- C++ Logical Operators
- C++ Bitwise Operators
- C++ Assignment Operators
- C++ sizeof Operator
- C++ Conditional Operator
- C++ Comma Operator
- C++ Member Operators
- C++ Casting Operators
- C++ Pointer Operators
- C++ Operators Precedence
- C++ Unary Operators
- C++ Control Statements
- C++ Decision Making
- C++ if Statement
- C++ if else Statement
- C++ Nested if Statements
- C++ switch Statement
- C++ Nested switch Statements
- C++ Loop Types
- C++ while Loop
- C++ for Loop
- C++ do while Loop
- C++ Foreach Loop
- C++ Nested Loops
- C++ break Statement
- C++ continue Statement
- C++ goto Statement
- C++ Strings
- C++ Strings
- C++ Loop Through a String
- C++ String Length
- C++ String Concatenation
- C++ String Comparison
- C++ Functions
- C++ Functions
- C++ Multiple Function Parameters
- C++ Recursive Function
- C++ Return Values
- C++ Function Overloading
- C++ Function Overriding
- C++ Default Arguments
- C++ Arrays
- C++ Arrays
- C++ Multidimensional Arrays
- C++ Pointer to an Array
- C++ Passing Arrays to Functions
- C++ Return Array from Functions
- C++ Structure & Union
- C++ Structures
- C++ Unions
- C++ Pointers
- C++ Pointers
- C++ Dereferencing
- C++ Modify Pointers
- C++ Class and Objects
- C++ Object Oriented
- C++ Classes & Objects
- C++ Class Member Functions
- C++ Class Access Modifiers
- C++ Static Class Members
- C++ Static Data Members
- C++ Static Member Function
- C++ Inline Functions
- C++ this Pointer
- C++ Friend Functions
- C++ Pointer to Classes
- C++ Constructors
- C++ Constructor & Destructor
- C++ Default Constructors
- C++ Parameterized Constructors
- C++ Copy Constructor
- C++ Constructor Overloading
- C++ Constructor with Default Arguments
- C++ Delegating Constructors
- C++ Constructor Initialization List
- C++ Dynamic Initialization Using Constructors
- C++ Object-oriented
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
- C++ Virtual Function
- C++ Pure Virtual Functions & Abstract Classes
- C++ File Handling
- C++ Files and Streams
- C++ Reading From File
- C++ Advanced
- C++ Exception Handling
- C++ Dynamic Memory
- C++ Namespaces
- C++ Templates
- C++ Preprocessor
- C++ Signal Handling
- C++ Multithreading
- C++ Web Programming
- C++ Socket Programming
- C++ Concurrency
- C++ Advanced Concepts
- C++ Lambda Expression
- C++ unordered_multiset
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. | \0 | 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"?