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

How Can I Print Unicode Characters in C ?

Patricia Arquette
Release: 2024-12-12 20:24:13
Original
738 people have browsed it

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;
}
Copy after login

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;
}
Copy after login

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;
}
Copy after login

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;
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template