C++ Char Data Types
A Char datatype is a datatype that is used to store a single character. It is always enclosed within a single quote (‘ ‘).
Syntax:
Char variable;
Example:
C++
// C++ Program demonstrate // Use of char #include <iostream> using namespace std; int main() { char c = 'g' ; cout << c; return 0; } |
g
ASCII Value
ASCII Value stands for American Standard Code for Information Interchange. It is used to represent the numeric value of all the characters.
ASCII Range of ‘a’ to ‘z’ = 97-122
ASCII Range of ‘A’ to ‘Z’ = 65-90
ASCII Range of ‘0’ to ‘9’ = 48-57
To know more about it, refer to the article – ASCII table.
Convert Character Value to Corresponding ASCII Value
To convert a character to ASCII value we have to typecast it using int(character) to get the corresponding numeric value.
Example:
C++
// C++ Program to convert // Char to ASCII value #include <iostream> using namespace std; int main() { char c = 'g' ; cout << "The Corresponding ASCII value of 'g' : " ; cout << int (c) << endl; c = 'A' ; cout << "The Corresponding ASCII value of 'A' : " ; cout << int (c) << endl; return 0; } |
The Corresponding ASCII value of 'g' : 103 The Corresponding ASCII value of 'A' : 65
Convert ASCII Value to Corresponding Character Value
To convert an ASCII value to a corresponding Character value we have to typecast it using char(int) to get the corresponding character value.
Example:
C++
// C++ Program to convert // ASCII value to character #include <iostream> using namespace std; int main() { int x = 53; cout << "The Corresponding character value of x is : " ; cout << char (x) << endl; x = 65; cout << "The Corresponding character value of x is : " ; cout << char (x) << endl; x = 97; cout << "The Corresponding character value of x is : " ; cout << char (x) << endl; return 0; } |
The Corresponding character value of x is : 5 The Corresponding character value of x is : A The Corresponding character value of x is : a
Escape Sequence in C++
Escape sequences are characters that determine how the line should be printed on the output window. The escape sequence always begins with a backslash ‘\’ (also known as an escape character). Some Examples of Escape Sequences are mentioned below:
S. No. | Escape Sequences | Character |
---|---|---|
1. | \n | Newline |
2. | \\ | Backslash |
3. | \t | Horizontal Tab |
4. | \v | Vertical Tab |
5. | \0 | Null Character |
Example:
C++
// C++ Program to demonstrate // Use of Escape Sequence #include <iostream> using namespace std; int main() { char a = 'G' ; // horizontal tab char b = '\t' ; char c = 'F' ; char d = '\t' ; char e = 'G' ; // new line char f = '\n' ; string s = "is the best" ; cout << a << b << c << d << e << f << s; return 0; } |
G F G is the best
Please Login to comment...