Home >Backend Development >C++ >How Can I Print Unicode Characters in C ?

How Can I Print Unicode Characters in C ?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-12 20:24:13741browse

How Can I Print Unicode Characters in C  ?

Printing Unicode Characters in C

Printing Unicode characters in C can seem like a daunting task. However, it is actually quite straightforward once you understand the concept.

To print the Russian character "ф" (U 0444), you can use Universal Character Names (UCNs). UCNs assign a unique identifier to each Unicode character. For instance, the UCN for "ф" is "u0444".

Here is an example of how to print "ф" using UCNs:

#include <iostream>

int main() {
    std::cout << "\u0444" << std::endl;
}

If your source code supports the character, you can also use the literal representation of the character:

#include <iostream>

int main() {
    char f = 'ф';
    std::cout << f << std::endl;
}

Additionally, if your console encoding supports the character, you can print it directly using the standard output stream:

#include <iostream>

int main() {
    std::cout << "Hello, ф!" << std::endl;
}

Printing Unicode characters can be slightly more complex in Windows environments. To ensure proper display, you may need to set the mode of the output file handle to accept UTF-16 data:

#include <iostream>
#include <io.h>
#include <fcntl.h>

int main() {
    _setmode(_fileno(stdout), _O_U16TEXT);
    std::wcout << L"Hello, ф!" << std::endl;
}

Remember that portable code may require a different approach.

The above is the detailed content of How Can I Print Unicode Characters in C ?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn