CL So
CL So

Reputation: 3759

How to find the built-in function to deal with char16_t in C?

Please tell what is the char16_t version for the String Manipulation Functions

such as:

http://www.tutorialspoint.com/ansi_c/c_function_references.htm

I found many references site, but no one mentioned that.

Especially for printing function, this is that most important, because it help me to verify whether the Manipulation function is work.

#include <stdio.h>
#include <uchar.h>



char16_t *u=u"α";
int main(int argc, char *argv[])
{
    printf("%x\n",u[0]); // output 3b1, it is UTF16



    wprintf("%s\n",u); //no ouput
    _cwprintf("%s\n",u); //incorrect output

    return 0;
}

Upvotes: 1

Views: 5166

Answers (2)

Hypersoft Systems
Hypersoft Systems

Reputation: 518

To print/read/open write etc.., you need to convert to 32-bit chars using the mbsrtowcs function.

For ALL intents and purposes, char16_t is a multi-byte representation, therefore, one need use mbr functions to work with this integral type.

A few answers used the L"prefix" which is completely incorrect. 16-bit strings require the u"prefix".

The following code gets you everything you need to work with 8, 16, and 32-bit string representations.

#include <string.h>
#include <wchar.h>
#include <uchar.h>

You can Google the procedures found in <wchar.h> if you don't have manual pages (UNIX).

Gnome.org's GLib has some great code for you to drop-in if overhead isn't an issue.

char16_t and char32_t are ISO C11 (iso9899:2011) extensions.

Upvotes: 2

egur
egur

Reputation: 7960

wprintf and its wchar colleagues need to have th format string in wchar too: wprintf( L"%s\n", u);

For wchar L is used as a prefix to the string literals.

Edit:

Here's a code snippet (tested on Windows):

#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <wchar.h>

void main()
{
    wchar_t* a = L"α";
    fflush(stdout); //must be done before _setmode
    _setmode(_fileno(stdout), _O_U16TEXT); // set console mode to unicode
    wprintf(L"alpha is:\n\t%s\n", a);      // works for me :)
}

The console doesn't work in unicode and prints a "?" for non ascii chars. In Linux you need to remove the underscore prefix before setmode and fileno.

Note: for windows GUI prints, there already proper support, so you can use wsprintf to format unicode strings.

Upvotes: 1

Related Questions