How to Print Extended Characters Using printf in C

I have attempted to print extended characters in C using printf and wprintf and have not been successful.  Simple line of code for a block (Unicode 2588) character is: wprintf (L"█");

Using setlocale has not helped. 

Whatever character I try to print, whether selected from Character Viewer or by using the Unicode number, appears to be masked to char size. 

Can someone please provide an example of how to accomplish this?


Mac mini, macOS 10.15

Posted on Mar 7, 2022 12:27 PM

Reply
Question marked as Top-ranking reply

Posted on Mar 9, 2022 9:03 AM

Thanks for verifying this on your system.


After reviewing my code I finally remembered something that is probably causing my problem (It has been a while since I looked at this project). I have an AppDelegate Class that has some Objective-C code (see below) that sets up a stdout pipe and notification to route printf output to a UI Window Text View. It is now clear to me that the printf output is being affected by the pipe when it comes to Unicode characters. All printf output to this view works correctly otherwise.




As you can see above , the string encoding parameter was set to NSASCIIStringEncoding.

I changed it to NSUTF8StringEncoding. Now it works perfectly!


Thank you both very much for your help.



10 replies
Question marked as Top-ranking reply

Mar 9, 2022 9:03 AM in response to MrHoffman

Thanks for verifying this on your system.


After reviewing my code I finally remembered something that is probably causing my problem (It has been a while since I looked at this project). I have an AppDelegate Class that has some Objective-C code (see below) that sets up a stdout pipe and notification to route printf output to a UI Window Text View. It is now clear to me that the printf output is being affected by the pipe when it comes to Unicode characters. All printf output to this view works correctly otherwise.




As you can see above , the string encoding parameter was set to NSASCIIStringEncoding.

I changed it to NSUTF8StringEncoding. Now it works perfectly!


Thank you both very much for your help.



Mar 7, 2022 5:24 PM in response to E. Seagull

In Terminal.app, open preferences > profiles > advanced, ensure your terminal is set to xterm-256color, and your text encoding set to Unicode UTF-8, and open a new tab, and try again.


#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
int main(void)
  {
  setlocale(LC_ALL, "");
  printf("❄️\n");
  printf("%s", u8"🍁\n");
  exit( EXIT_SUCCESS );
  }



$ /Users/MrHoffman/Library/Developer/Xcode/DerivedData/.../C\ tests
❄️
🍁
$ locale
LANG="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_ALL=
$ 


With C, you get to tangle directly with Unicode, with ASCII and its UTF-8 superset, and with wchar_t encoding for both. Given my choice here, I'd run with UTF-8 directly and avoid wchar_t. UTF-8 is multi-byte, so don't assume that one character, err, that one glyph is one byte, because that only works with ASCII, and which means strlen and sizeof and buffer allocations can all provide run-time surprises.


This is easier in ObjC and Swift, if either are options here.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to Print Extended Characters Using printf in C

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.