printf a wchar_t?
Luc Rooijakkers
lwj at cs.kun.nl
Tue Jan 29 21:24:22 AEST 1991
In <1991Jan25.014927.24195 at contact.uucp> egr at contact.uucp (Gordan Palameta) writes:
>Can you print a wchar_t using printf and some sort of % format?
>Is this a reasonable thing to want to do at all?
>K&R II and H&S seem to say nothing about this...
My interpretation of the standard says: no. While it would have been
easy for the committee to include a %lc and a %ls format for printf, they
have not done this. This would not impose any significant overheads for
implementations that have MB_LEN_MAX==1 (see my companion article about
multibyte characters in printf/scanf formats).
You can use the following function, though:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
char *wc2mb(wchar_t wch)
{
static char mbch[MB_LEN_MAX+1];
int mbchlen;
/* reset to initial shift state */
wctomb ( (char *)NULL, L'\0' );
/* convert wch to multibyte character */
mbchlen = wctomb(mbch,wch);
/* if not a valid multibyte character, return empty string */
if ( mbchlen < 0 )
return "";
/* append null character */
mbch[mbchlen] = '\0';
/* return the multibyte character */
return mbch;
}
and then use wc2mb(wch) with a %s format specifier.
--
Luc Rooijakkers Internet: lwj at cs.kun.nl
Faculty of Mathematics and Computer Science UUCP: uunet!cs.kun.nl!lwj
University of Nijmegen, the Netherlands tel. +3180652271
More information about the Comp.std.c
mailing list