Overview of C++ Character Sets

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 &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;locale&gt;

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 &lt;&lt; "French: Let them eat g\u00E2teau.\n";  // gâteau (cake)
    cout &lt;&lt; "French: Cr\u00E8me br\u00FBl\u00E9e\n"; // Crème brûlée
    
    // German example
    cout &lt;&lt; "German: Stra\u00DFe\n";  // Straße (street)
    cout &lt;&lt; "German: M\u00FCnchen\n"; // München (Munich)
    
    // Chinese example (requires more digits)
    cout &lt;&lt; "Chinese: \u4E2D\u6587\n";  // 中文
    cout &lt;&lt; "Chinese: \u6B22\u8FCE\n";  // 欢迎
    
    // Japanese example
    cout &lt;&lt; "Japanese: \u3053\u3093\u306B\u3061\u306F\n";  // こんにちは (Hello)
    
    // Russian example
    cout &lt;&lt; "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 &lt;&lt; "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&amp; n, double p) : name(n), price(p) {}
    
    void display() {
        cout &lt;&lt; "Caf\u00E9: " &lt;&lt; name &lt;&lt; ", Price: $" &lt;&lt; price &lt;&lt; endl;
    }
    
    // Method with accent
    void pr\u00E9senter() {
        cout &lt;&lt; "Pr\u00E9sentation du " &lt;&lt; name &lt;&lt; endl;
    }
};

// Example 5: Mathematical symbols and special characters
void mathAndSymbols() {
    // Mathematical symbols
    cout &lt;&lt; "Math: \u03C0 = " &lt;&lt; 3.14159 &lt;&lt; endl;  // π
    cout &lt;&lt; "Math: \u2211 x\u00B2\n";  // ∑ x²
    cout &lt;&lt; "Math: \u221E\n";  // ∞
    
    // Currency symbols
    cout &lt;&lt; "Currency: \u20AC \u00A3 \u00A5 \u20B9\n";  // € £ ¥ ₹
    
    // Arrow symbols
    cout &lt;&lt; "Arrows: \u2190 \u2192 \u2191 \u2193\n";  // ← → ↑ ↓
}

// Example 6: Unicode in file operations
#include &lt;fstream&gt;
void unicodeFileOperation() {
    // Create a file containing Unicode characters
    ofstream file("unicode_example.txt");
    if (file.is_open()) {
        file &lt;&lt; "Multilingual Text:\n";
        file &lt;&lt; "French: Caf\u00E9 au lait\n";
        file &lt;&lt; "German: Stra\u00DFenbahn\n";
        file &lt;&lt; "Chinese: \u4E2D\u56FD\n";
        file &lt;&lt; "Japanese: \u65E5\u672C\u8A9E\n";
        file.close();
        cout &lt;&lt; "Unicode file created successfully!\n";
    }
}

int main() {
    // Set locale to support Unicode output
    setlocale(LC_ALL, "en_US.UTF-8");
    
    cout &lt;&lt; "=== C++ Universal Character Name Demo ===\n\n";
    
    // Test international class
    InternationalClass obj;
    obj.setValue(42);
    cout &lt;&lt; "Value from InternationalClass: " &lt;&lt; obj.getValue() &lt;&lt; endl;
    
    cout &lt;&lt; "\n--- International Strings ---\n";
    printInternationalStrings();
    
    cout &lt;&lt; "\n--- Unicode Function Name ---\n";
    \u0068\u0065\u006C\u006C\u006FWorld();  // Call helloWorld function
    
    cout &lt;&lt; "\n--- Café Class Demo ---\n";
    Caf\u00E9 myCafe("Latte", 4.50);
    myCafe.display();
    myCafe.pr\u00E9senter();
    
    cout &lt;&lt; "\n--- Math and Symbols ---\n";
    mathAndSymbols();
    
    cout &lt;&lt; "\n--- File Operations ---\n";
    unicodeFileOperation();
    
    // Example 7: Using Unicode in loops
    cout &lt;&lt; "\n--- Unicode in Loops ---\n";
    string unicodeChars[] = {
        "\u2665",  // ♥
        "\u2605",  // ★
        "\u263A",  // ☺
        "\u2708",  // ✈
        "\u2764"   // ❤
    };
    
    for (const auto&amp; ch : unicodeChars) {
        cout &lt;&lt; "Character: " &lt;&lt; ch &lt;&lt; " ";
    }
    cout &lt;&lt; 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

  1. Compiler Support: Ensure the compiler supports C++11 or higher standards
  2. Terminal Settings: The terminal must support UTF-8 encoding to display Unicode characters correctly
  3. Font Support: The terminal font must include the corresponding Unicode characters
  4. 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.

Overview of C++ Character Sets

Leave a Comment