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.










share|improve this question





















  • Do you have trouble with more common Unicode characters?
    – chux
    May 7 '17 at 17:12






  • 1




    @chux Well, I can print a with value 0x0061 but not ǎ with value 0x01ce.
    – Luke Collins
    May 7 '17 at 17:17














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.










share|improve this question





















  • Do you have trouble with more common Unicode characters?
    – chux
    May 7 '17 at 17:12






  • 1




    @chux Well, I can print a with value 0x0061 but not ǎ with value 0x01ce.
    – Luke Collins
    May 7 '17 at 17:17












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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 print a with value 0x0061 but not ǎ with value 0x01ce.
    – Luke Collins
    May 7 '17 at 17:17
















  • Do you have trouble with more common Unicode characters?
    – chux
    May 7 '17 at 17:12






  • 1




    @chux Well, I can print a with value 0x0061 but not ǎ with value 0x01ce.
    – 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












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);






share|improve this answer




















  • 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


















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 using waddwstr, or

  • storing it in a cchar_t structure (with setcchar), and using wadd_wch with that as a parameter, or

  • converting the wchar_t to a multibyte string, and using waddstr





share|improve this answer




















  • 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











  • can you explain a bit more?
    – Luke Collins
    May 7 '17 at 18:13






  • 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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















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

























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);






share|improve this answer




















  • 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















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);






share|improve this answer




















  • 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













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);






share|improve this answer












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);







share|improve this answer












share|improve this answer



share|improve this answer










answered May 7 '17 at 17:15









Antti Haapala

79.2k16148192




79.2k16148192











  • 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

















  • 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
















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













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 using waddwstr, or

  • storing it in a cchar_t structure (with setcchar), and using wadd_wch with that as a parameter, or

  • converting the wchar_t to a multibyte string, and using waddstr





share|improve this answer




















  • 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











  • can you explain a bit more?
    – Luke Collins
    May 7 '17 at 18:13






  • 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














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 using waddwstr, or

  • storing it in a cchar_t structure (with setcchar), and using wadd_wch with that as a parameter, or

  • converting the wchar_t to a multibyte string, and using waddstr





share|improve this answer




















  • 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











  • can you explain a bit more?
    – Luke Collins
    May 7 '17 at 18:13






  • 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












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 using waddwstr, or

  • storing it in a cchar_t structure (with setcchar), and using wadd_wch with that as a parameter, or

  • converting the wchar_t to a multibyte string, and using waddstr





share|improve this answer












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 using waddwstr, or

  • storing it in a cchar_t structure (with setcchar), and using wadd_wch with that as a parameter, or

  • converting the wchar_t to a multibyte string, and using waddstr






share|improve this answer












share|improve this answer



share|improve this answer










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-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











  • can you explain a bit more?
    – Luke Collins
    May 7 '17 at 18:13






  • 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
















  • 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











  • can you explain a bit more?
    – Luke Collins
    May 7 '17 at 18:13






  • 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















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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto