How to initialize wchar_t variable?

I am reading a book: C: In a nutshell, and after reading the section "Character Sets", which talks about wide characters, I wrote this program:

#include <stdio.h> #include <stddef.h> #include <wchar.h> int main() { wchar_t wc = '\x3b1'; wprintf(L"%lc\n", wc); return 0; } 

Then I compiled it with gcc, but gcc gave me this warning:

main.c: 7: 15: warning: hexadecimal escape sequence out of range [enabled by default]

And the program does not display the character α (whose unicode is U + 03B1), which I wanted to do.

How to change the program for printing the character α?

+7
source share
3 answers

It works for me

 #include <stdio.h> #include <stddef.h> #include <wchar.h> #include <locale.h> int main(void) { wchar_t wc = L'\x3b1'; setlocale(LC_ALL, "en_US.UTF-8"); wprintf(L"%lc\n", wc); return 0; } 
+5
source
 wchar_t wc = L'\x3b1'; 

is the correct way to initialize the wchar_t variable for U + 03B1. The prefix L is used to indicate the literal wchar_t. Your code defines the char literal and why the compiler warns.

The fact that you do not see the desired character when printing does not match the settings for the local environment settings.

+5
source

try L'\x03B1' This may solve your problem. IF you doubt you can try:

 '\u03b1' to initialize. 
0
source

All Articles