1. Basic Source Character Set
- Characters on a standard US keyboard (uppercase and lowercase letters, numbers)
- C language symbols (
<span>&</span>,<span>=</span>,<span>+</span>,<span>-</span>, etc.) - Control characters (newline, space, etc.)
2. Execution Character Set
- Characters processed during program execution
- Includes control characters like backspace and bell
3. Universal Character Name (UCN)
C++ uses Universal Character Names to represent special characters, formatted as:
<span>\u</span>+ 8-digit hexadecimal<span>\U</span>+ 16-digit hexadecimal
Code Examples
#include <iostream>
#include <string>
#include <locale>
using namespace std;
// Example 1: Using Universal Character Names in identifiers
class InternationalClass {
public:
// Using Universal Character Names as variable names
int k\u00F6rper; // körper (German: body)
string na\u0308he; // nähe (German: proximity)
void setValue(int value) {
k\u00F6rper = value;
}
int getValue() {
return k\u00F6rper;
}
};
// Example 2: Using Universal Character Names in strings
void printInternationalStrings() {
// French example
cout << "French: Let them eat g\u00E2teau.\n"; // gâteau (cake)
cout << "French: Cr\u00E8me br\u00FBl\u00E9e\n"; // Crème brûlée
// German example
cout << "German: Stra\u00DFe\n"; // Straße (street)
cout << "German: M\u00FCnchen\n"; // München (Munich)
// Chinese example (requires more digits)
cout << "Chinese: \u4E2D\u6587\n"; // 中文
cout << "Chinese: \u6B22\u8FCE\n"; // 欢迎
// Japanese example
cout << "Japanese: \u3053\u3093\u306B\u3061\u306F\n"; // こんにちは (Hello)
// Russian example
cout << "Russian: \u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435\n"; // Здравствуйте (Hello)
}
// Example 3: Using in function names
void \u0068\u0065\u006C\u006C\u006FWorld() {
cout << "Hello World from function with Unicode name!\n";
}
// Example 4: Complete class example
class Caf\u00E9 { // Café class
private:
string name;
double price;
public:
Caf\u00E9(const string& n, double p) : name(n), price(p) {}
void display() {
cout << "Caf\u00E9: " << name << ", Price: $" << price << endl;
}
// Method with accent
void pr\u00E9senter() {
cout << "Pr\u00E9sentation du " << name << endl;
}
};
// Example 5: Mathematical symbols and special characters
void mathAndSymbols() {
// Mathematical symbols
cout << "Math: \u03C0 = " << 3.14159 << endl; // π
cout << "Math: \u2211 x\u00B2\n"; // ∑ x²
cout << "Math: \u221E\n"; // ∞
// Currency symbols
cout << "Currency: \u20AC \u00A3 \u00A5 \u20B9\n"; // € £ ¥ ₹
// Arrow symbols
cout << "Arrows: \u2190 \u2192 \u2191 \u2193\n"; // ← → ↑ ↓
}
// Example 6: Unicode in file operations
#include <fstream>
void unicodeFileOperation() {
// Create a file containing Unicode characters
ofstream file("unicode_example.txt");
if (file.is_open()) {
file << "Multilingual Text:\n";
file << "French: Caf\u00E9 au lait\n";
file << "German: Stra\u00DFenbahn\n";
file << "Chinese: \u4E2D\u56FD\n";
file << "Japanese: \u65E5\u672C\u8A9E\n";
file.close();
cout << "Unicode file created successfully!\n";
}
}
int main() {
// Set locale to support Unicode output
setlocale(LC_ALL, "en_US.UTF-8");
cout << "=== C++ Universal Character Name Demo ===\n\n";
// Test international class
InternationalClass obj;
obj.setValue(42);
cout << "Value from InternationalClass: " << obj.getValue() << endl;
cout << "\n--- International Strings ---\n";
printInternationalStrings();
cout << "\n--- Unicode Function Name ---\n";
\u0068\u0065\u006C\u006C\u006FWorld(); // Call helloWorld function
cout << "\n--- Café Class Demo ---\n";
Caf\u00E9 myCafe("Latte", 4.50);
myCafe.display();
myCafe.pr\u00E9senter();
cout << "\n--- Math and Symbols ---\n";
mathAndSymbols();
cout << "\n--- File Operations ---\n";
unicodeFileOperation();
// Example 7: Using Unicode in loops
cout << "\n--- Unicode in Loops ---\n";
string unicodeChars[] = {
"\u2665", // ♥
"\u2605", // ★
"\u263A", // ☺
"\u2708", // ✈
"\u2764" // ❤
};
for (const auto& ch : unicodeChars) {
cout << "Character: " << ch << " ";
}
cout << endl;
return 0;
}
Compilation and Running Instructions
Compilation Command (Linux/Mac):
g++ -std=c++11 -o unicode_demo unicode_demo.cpp
./unicode_demo
Compilation Command (Windows):
g++ -std=c++11 -o unicode_demo.exe unicode_demo.cpp
unicode_demo.exe
Important Notes
- Compiler Support: Ensure the compiler supports C++11 or higher standards
- Terminal Settings: The terminal must support UTF-8 encoding to display Unicode characters correctly
- Font Support: The terminal font must include the corresponding Unicode characters
- Portability: Using Unicode characters in identifiers may affect the portability of the code
Unicode Code Point Ranges
Examples of commonly used Unicode code points:
- Latin Extended: U+0080 – U+00FF
- Greek Letters: U+0370 – U+03FF
- Cyrillic Letters: U+0400 – U+04FF
- CJK Unified Ideographs: U+4E00 – U+9FFF
- Mathematical Symbols: U+2200 – U+22FF
This example demonstrates how C++ supports internationalized programming through Universal Character Names, allowing the code to handle characters from various languages.
