🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #2340

Re: printf a wchar_t?

#2340
From: Luc Rooijakkers
Date: Tue, 29 Jan 1991 10:24
51 lines
1865 bytes
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
Technical Details
Message-ID: <2699@wn1.sci.kun.nl>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!samsung!uunet!mcsun!hp4nl!sci.kun.nl!cs.kun.nl!lwj
References: <1991Jan25.014927.24195@contact.uucp>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!samsung!uunet!mcsun!hp4nl!sci.kun.nl!cs.kun.nl!lwj
From: lwj@cs.kun.nl (Luc Rooijakkers)
Newsgroups: comp.std.c
Subject: Re: printf a wchar_t?
Message-ID: <2699@wn1.sci.kun.nl>
Date: 29 Jan 91 10:24:22 GMT
References: <1991Jan25.014927.24195@contact.uucp>
Sender: root@sci.kun.nl
Organization: University of Nijmegen, The Netherlands
Lines: 51