Printing a Unicode Symbol in C
up vote
3
down vote
favorite
I'm trying to print a unicode star character (0x2605) in a linux terminal using C. I've followed the syntax suggested by other answers on the site, but I'm not getting an output:
#include <stdio.h>
#include <wchar.h>
int main()
wchar_t star = 0x2605;
wprintf(L"%cn", star);
return 0;
I'd appreciate any suggestions, especially how I can make this work with the ncurses
library.
c unicode ncurses
add a comment |
up vote
3
down vote
favorite
I'm trying to print a unicode star character (0x2605) in a linux terminal using C. I've followed the syntax suggested by other answers on the site, but I'm not getting an output:
#include <stdio.h>
#include <wchar.h>
int main()
wchar_t star = 0x2605;
wprintf(L"%cn", star);
return 0;
I'd appreciate any suggestions, especially how I can make this work with the ncurses
library.
c unicode ncurses
Do you have trouble with more common Unicode characters?
– chux
May 7 '17 at 17:12
1
@chux Well, I can printa
with value0x0061
but notǎ
with value0x01ce
.
– Luke Collins
May 7 '17 at 17:17
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I'm trying to print a unicode star character (0x2605) in a linux terminal using C. I've followed the syntax suggested by other answers on the site, but I'm not getting an output:
#include <stdio.h>
#include <wchar.h>
int main()
wchar_t star = 0x2605;
wprintf(L"%cn", star);
return 0;
I'd appreciate any suggestions, especially how I can make this work with the ncurses
library.
c unicode ncurses
I'm trying to print a unicode star character (0x2605) in a linux terminal using C. I've followed the syntax suggested by other answers on the site, but I'm not getting an output:
#include <stdio.h>
#include <wchar.h>
int main()
wchar_t star = 0x2605;
wprintf(L"%cn", star);
return 0;
I'd appreciate any suggestions, especially how I can make this work with the ncurses
library.
c unicode ncurses
c unicode ncurses
asked May 7 '17 at 17:07
Luke Collins
617420
617420
Do you have trouble with more common Unicode characters?
– chux
May 7 '17 at 17:12
1
@chux Well, I can printa
with value0x0061
but notǎ
with value0x01ce
.
– Luke Collins
May 7 '17 at 17:17
add a comment |
Do you have trouble with more common Unicode characters?
– chux
May 7 '17 at 17:12
1
@chux Well, I can printa
with value0x0061
but notǎ
with value0x01ce
.
– Luke Collins
May 7 '17 at 17:17
Do you have trouble with more common Unicode characters?
– chux
May 7 '17 at 17:12
Do you have trouble with more common Unicode characters?
– chux
May 7 '17 at 17:12
1
1
@chux Well, I can print
a
with value 0x0061
but not ǎ
with value 0x01ce
.– Luke Collins
May 7 '17 at 17:17
@chux Well, I can print
a
with value 0x0061
but not ǎ
with value 0x01ce
.– Luke Collins
May 7 '17 at 17:17
add a comment |
2 Answers
2
active
oldest
votes
up vote
5
down vote
accepted
Two problems: first of all, a wchar_t
must be printed with %lc
format, not %c
. The second one is that unless you call setlocale
the character set is not set properly, and you probably get ?
instead of your star. The following code seems to work though:
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main()
setlocale(LC_CTYPE, "");
wchar_t star = 0x2605;
wprintf(L"%lcn", star);
This fixed it, thanks! ★ Any ideas how tomvwprint
this withncurses
though?
– Luke Collins
May 7 '17 at 17:18
stackoverflow.com/questions/15222466/…
– Antti Haapala
May 7 '17 at 17:46
add a comment |
up vote
1
down vote
Whether you are using stdio or ncurses, you have to initialize the locale, as noted in the ncurses manual. Otherwise, multibyte encodings such as UTF-8 do not work.
wprintw
doesn't necessarily know about wchar_t
(though it may use the same underlying printf
, this depends on the platform and configuration).
With ncurses, you would display a wchar_t
in any of these ways:
- storing it in an array of
wchar_t
, and usingwaddwstr
, or - storing it in a
cchar_t
structure (withsetcchar
), and usingwadd_wch
with that as a parameter, or - converting the
wchar_t
to a multibyte string, and usingwaddstr
Thanks for the reply. I keep getting an implicit declaration error for the wide-characterncurses
functions, are there some other libraries I need to be including?
– Luke Collins
May 7 '17 at 17:45
You're missing a#define
. Generally that should be _XOPEN_SOURCE_EXTENDED, but most platforms have fubar'd ifdef's: for Linux just use
-D_GNU_SOURCE. You'll have to link with
-lncursesw`, but that's not implicit declaration
– Thomas Dickey
May 7 '17 at 18:04
can you explain a bit more?
– Luke Collins
May 7 '17 at 18:13
1
It would be defined usingncursesw5-config --cflags
, or via a ".pc" file, depending on how ncurses is packaged for your system.
– Thomas Dickey
May 7 '17 at 18:37
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
Two problems: first of all, a wchar_t
must be printed with %lc
format, not %c
. The second one is that unless you call setlocale
the character set is not set properly, and you probably get ?
instead of your star. The following code seems to work though:
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main()
setlocale(LC_CTYPE, "");
wchar_t star = 0x2605;
wprintf(L"%lcn", star);
This fixed it, thanks! ★ Any ideas how tomvwprint
this withncurses
though?
– Luke Collins
May 7 '17 at 17:18
stackoverflow.com/questions/15222466/…
– Antti Haapala
May 7 '17 at 17:46
add a comment |
up vote
5
down vote
accepted
Two problems: first of all, a wchar_t
must be printed with %lc
format, not %c
. The second one is that unless you call setlocale
the character set is not set properly, and you probably get ?
instead of your star. The following code seems to work though:
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main()
setlocale(LC_CTYPE, "");
wchar_t star = 0x2605;
wprintf(L"%lcn", star);
This fixed it, thanks! ★ Any ideas how tomvwprint
this withncurses
though?
– Luke Collins
May 7 '17 at 17:18
stackoverflow.com/questions/15222466/…
– Antti Haapala
May 7 '17 at 17:46
add a comment |
up vote
5
down vote
accepted
up vote
5
down vote
accepted
Two problems: first of all, a wchar_t
must be printed with %lc
format, not %c
. The second one is that unless you call setlocale
the character set is not set properly, and you probably get ?
instead of your star. The following code seems to work though:
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main()
setlocale(LC_CTYPE, "");
wchar_t star = 0x2605;
wprintf(L"%lcn", star);
Two problems: first of all, a wchar_t
must be printed with %lc
format, not %c
. The second one is that unless you call setlocale
the character set is not set properly, and you probably get ?
instead of your star. The following code seems to work though:
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main()
setlocale(LC_CTYPE, "");
wchar_t star = 0x2605;
wprintf(L"%lcn", star);
answered May 7 '17 at 17:15
Antti Haapala
79.2k16148192
79.2k16148192
This fixed it, thanks! ★ Any ideas how tomvwprint
this withncurses
though?
– Luke Collins
May 7 '17 at 17:18
stackoverflow.com/questions/15222466/…
– Antti Haapala
May 7 '17 at 17:46
add a comment |
This fixed it, thanks! ★ Any ideas how tomvwprint
this withncurses
though?
– Luke Collins
May 7 '17 at 17:18
stackoverflow.com/questions/15222466/…
– Antti Haapala
May 7 '17 at 17:46
This fixed it, thanks! ★ Any ideas how to
mvwprint
this with ncurses
though?– Luke Collins
May 7 '17 at 17:18
This fixed it, thanks! ★ Any ideas how to
mvwprint
this with ncurses
though?– Luke Collins
May 7 '17 at 17:18
stackoverflow.com/questions/15222466/…
– Antti Haapala
May 7 '17 at 17:46
stackoverflow.com/questions/15222466/…
– Antti Haapala
May 7 '17 at 17:46
add a comment |
up vote
1
down vote
Whether you are using stdio or ncurses, you have to initialize the locale, as noted in the ncurses manual. Otherwise, multibyte encodings such as UTF-8 do not work.
wprintw
doesn't necessarily know about wchar_t
(though it may use the same underlying printf
, this depends on the platform and configuration).
With ncurses, you would display a wchar_t
in any of these ways:
- storing it in an array of
wchar_t
, and usingwaddwstr
, or - storing it in a
cchar_t
structure (withsetcchar
), and usingwadd_wch
with that as a parameter, or - converting the
wchar_t
to a multibyte string, and usingwaddstr
Thanks for the reply. I keep getting an implicit declaration error for the wide-characterncurses
functions, are there some other libraries I need to be including?
– Luke Collins
May 7 '17 at 17:45
You're missing a#define
. Generally that should be _XOPEN_SOURCE_EXTENDED, but most platforms have fubar'd ifdef's: for Linux just use
-D_GNU_SOURCE. You'll have to link with
-lncursesw`, but that's not implicit declaration
– Thomas Dickey
May 7 '17 at 18:04
can you explain a bit more?
– Luke Collins
May 7 '17 at 18:13
1
It would be defined usingncursesw5-config --cflags
, or via a ".pc" file, depending on how ncurses is packaged for your system.
– Thomas Dickey
May 7 '17 at 18:37
add a comment |
up vote
1
down vote
Whether you are using stdio or ncurses, you have to initialize the locale, as noted in the ncurses manual. Otherwise, multibyte encodings such as UTF-8 do not work.
wprintw
doesn't necessarily know about wchar_t
(though it may use the same underlying printf
, this depends on the platform and configuration).
With ncurses, you would display a wchar_t
in any of these ways:
- storing it in an array of
wchar_t
, and usingwaddwstr
, or - storing it in a
cchar_t
structure (withsetcchar
), and usingwadd_wch
with that as a parameter, or - converting the
wchar_t
to a multibyte string, and usingwaddstr
Thanks for the reply. I keep getting an implicit declaration error for the wide-characterncurses
functions, are there some other libraries I need to be including?
– Luke Collins
May 7 '17 at 17:45
You're missing a#define
. Generally that should be _XOPEN_SOURCE_EXTENDED, but most platforms have fubar'd ifdef's: for Linux just use
-D_GNU_SOURCE. You'll have to link with
-lncursesw`, but that's not implicit declaration
– Thomas Dickey
May 7 '17 at 18:04
can you explain a bit more?
– Luke Collins
May 7 '17 at 18:13
1
It would be defined usingncursesw5-config --cflags
, or via a ".pc" file, depending on how ncurses is packaged for your system.
– Thomas Dickey
May 7 '17 at 18:37
add a comment |
up vote
1
down vote
up vote
1
down vote
Whether you are using stdio or ncurses, you have to initialize the locale, as noted in the ncurses manual. Otherwise, multibyte encodings such as UTF-8 do not work.
wprintw
doesn't necessarily know about wchar_t
(though it may use the same underlying printf
, this depends on the platform and configuration).
With ncurses, you would display a wchar_t
in any of these ways:
- storing it in an array of
wchar_t
, and usingwaddwstr
, or - storing it in a
cchar_t
structure (withsetcchar
), and usingwadd_wch
with that as a parameter, or - converting the
wchar_t
to a multibyte string, and usingwaddstr
Whether you are using stdio or ncurses, you have to initialize the locale, as noted in the ncurses manual. Otherwise, multibyte encodings such as UTF-8 do not work.
wprintw
doesn't necessarily know about wchar_t
(though it may use the same underlying printf
, this depends on the platform and configuration).
With ncurses, you would display a wchar_t
in any of these ways:
- storing it in an array of
wchar_t
, and usingwaddwstr
, or - storing it in a
cchar_t
structure (withsetcchar
), and usingwadd_wch
with that as a parameter, or - converting the
wchar_t
to a multibyte string, and usingwaddstr
answered May 7 '17 at 17:38
Thomas Dickey
30.8k62659
30.8k62659
Thanks for the reply. I keep getting an implicit declaration error for the wide-characterncurses
functions, are there some other libraries I need to be including?
– Luke Collins
May 7 '17 at 17:45
You're missing a#define
. Generally that should be _XOPEN_SOURCE_EXTENDED, but most platforms have fubar'd ifdef's: for Linux just use
-D_GNU_SOURCE. You'll have to link with
-lncursesw`, but that's not implicit declaration
– Thomas Dickey
May 7 '17 at 18:04
can you explain a bit more?
– Luke Collins
May 7 '17 at 18:13
1
It would be defined usingncursesw5-config --cflags
, or via a ".pc" file, depending on how ncurses is packaged for your system.
– Thomas Dickey
May 7 '17 at 18:37
add a comment |
Thanks for the reply. I keep getting an implicit declaration error for the wide-characterncurses
functions, are there some other libraries I need to be including?
– Luke Collins
May 7 '17 at 17:45
You're missing a#define
. Generally that should be _XOPEN_SOURCE_EXTENDED, but most platforms have fubar'd ifdef's: for Linux just use
-D_GNU_SOURCE. You'll have to link with
-lncursesw`, but that's not implicit declaration
– Thomas Dickey
May 7 '17 at 18:04
can you explain a bit more?
– Luke Collins
May 7 '17 at 18:13
1
It would be defined usingncursesw5-config --cflags
, or via a ".pc" file, depending on how ncurses is packaged for your system.
– Thomas Dickey
May 7 '17 at 18:37
Thanks for the reply. I keep getting an implicit declaration error for the wide-character
ncurses
functions, are there some other libraries I need to be including?– Luke Collins
May 7 '17 at 17:45
Thanks for the reply. I keep getting an implicit declaration error for the wide-character
ncurses
functions, are there some other libraries I need to be including?– Luke Collins
May 7 '17 at 17:45
You're missing a
#define
. Generally that should be _XOPEN_SOURCE_EXTENDED, but most platforms have fubar'd ifdef's: for Linux just use
-D_GNU_SOURCE. You'll have to link with
-lncursesw`, but that's not implicit declaration– Thomas Dickey
May 7 '17 at 18:04
You're missing a
#define
. Generally that should be _XOPEN_SOURCE_EXTENDED, but most platforms have fubar'd ifdef's: for Linux just use
-D_GNU_SOURCE. You'll have to link with
-lncursesw`, but that's not implicit declaration– Thomas Dickey
May 7 '17 at 18:04
can you explain a bit more?
– Luke Collins
May 7 '17 at 18:13
can you explain a bit more?
– Luke Collins
May 7 '17 at 18:13
1
1
It would be defined using
ncursesw5-config --cflags
, or via a ".pc" file, depending on how ncurses is packaged for your system.– Thomas Dickey
May 7 '17 at 18:37
It would be defined using
ncursesw5-config --cflags
, or via a ".pc" file, depending on how ncurses is packaged for your system.– Thomas Dickey
May 7 '17 at 18:37
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43834315%2fprinting-a-unicode-symbol-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Do you have trouble with more common Unicode characters?
– chux
May 7 '17 at 17:12
1
@chux Well, I can print
a
with value0x0061
but notǎ
with value0x01ce
.– Luke Collins
May 7 '17 at 17:17