Characters in C++

Notes:

Characters in C++ Programming Language :

Character:
- A symbol or sequence of symbols enclosed in between pair of single quotations is considered as a character in C++.

C++ allows us to work with both ASCII character set as well as Unicode character set.

ASCII character set:
- ASCII stands for American Standards Code for Information Interchange
- ASCII is a 7 bits character set
- Using ASCII character set we can represent maximum 128 characters
- ASCII character set has a unique number associated to each character in it
- To store and process ASCII characters we take help of char data type.
- char data type is of 1 byte (i.e. 8 bits); which is sufficient to store and process all ASCII characters

Syntax for declaring and initializing a character type variable:
char nameOfVariable = ’value’;
Ex:
char ch = ’A’;
where
- value can be any symbol from the ASCII character set

Example Code:

#include <iostream>
using namespace std;
int main()
{
char ch = 'A';
// OR
// char ch = (char) 65;
cout << ch << endl; // A
return 0;
}

Unicode character set:
- Unicode stands for Universal code
- Unicode is a variable length character set (i.e. 8bits, 16bits or 32bits)
- Using Unicode character set we can represent almost all characters from almost all written languages present in the world
- Unicode character set has a unique code associated to each character in it
- To store and process Unicode characters we take help of wchar_t data type.
- wchar_t is of 2 bytes (i.e. 16 bits); which is sufficient to store and process more than 65K+ characters

Syntax for declaring and initializing a wide character type variable:
wchar_t nameOfVariable = L’\u 4digit code’;
Ex:
wchar_t ch = L’\u0041’; // A

Example Code:

#include <iostream>
#include<stdio.h>
#include<io.h>
#define _O_TEXT 0x4000
#define _O_WTEXT 0x10000
using namespace std;

int main()
{

_setmode(_fileno(stdout),_O_TEXT);
cout << "First alphabet = ";

_setmode(_fileno(stdout),_O_WTEXT);
wchar_t ch = L'\u0041';
wcout << ch;

_setmode(_fileno(stdout),_O_TEXT);
cout << endl;

return 0;
}