[comp.std.c] printf a wchar_t?

egr@contact.uucp (Gordan Palameta) (01/25/91)

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

lwj@cs.kun.nl (Luc Rooijakkers) (01/29/91)

In <1991Jan25.014927.24195@contact.uucp> egr@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@cs.kun.nl
Faculty of Mathematics and Computer Science     UUCP: uunet!cs.kun.nl!lwj
University of Nijmegen, the Netherlands         tel. +3180652271

gwyn@smoke.brl.mil (Doug Gwyn) (01/30/91)

In article <2699@wn1.sci.kun.nl> lwj@cs.kun.nl (Luc Rooijakkers) writes:
>>Can you print a wchar_t using printf and some sort of % format?
>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.

That's because wchar_t is purely for internal program use, for example
in converting multibyte sequences to individual "character" units that
can be handled using traditional C programming methods, and it is
expected that the internal wchar_t representations would be mapped
back into multibyte sequences before being presented to the external
environment.  There are "shift state" reasons for not wanting to do
this at the individual wchar_t level, but rather on a 0-terminated
array of wchar_t.

keie@cs.vu.nl (Keizer E G) (01/30/91)

egr@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...

When X3J11 added wchar_t's to the C standard the committee knew that the
standard would not offer the complete range of functionality. One of
the criteria for adding features to the standard was "add only features
that have been implemented in one or more compilers and used". X3J11
was aware of the fact that there was not sufficient experience with
wchar_t's to do add the full range of functionality.

WG14, X3J11's counterpart on the international level, is considering
a proposal by the Japanese to extend the functionality of wchar_t's.
One part of this proposal will be a printf for wchar_t's. Due to the slow
process of standardization it will be some time before this proposal
reaches the status of official addendum to the C standard.

Ed Keizer
Member of WG14.

This article does present my personal views, not those of X3J11 or WG14.