Here a nice little code example that you can show to people interested
in character set aware programming to demonstrate how simple properly
written portable code to output a certain character in a
locale-dependent portable way can look like:

-------------------------------------------------------------------
/* Trivial demonstration of how to output a Unicode character (here:
 * the euro sign, U+20AC) with ISO C in a portable way. [For Linux,
 * make sure you have glibc 2.2 or newer, otherwise you'll only get
 * the ASCII fallback.] */

#include <stdio.h>
#include <locale.h>

int main() {

  /* Set the locale, such that C's multibyte output routines know,
   * which encoding (ISO 8859-1, ISO 8859-15, UTF-8, ASCII, etc.)
   * the user wants */
  if (!setlocale(LC_CTYPE, "")) {
    fprintf(stderr, "Can't set the specified locale! "
            "Check LANG, LC_CTYPE, LC_ALL.\n");
    return 1;
  }
  
  printf("Euro sign: ");

#ifdef __STDC_ISO_10646__
  if (printf("%lc", 0x20ac) < 0)   /* try C's multibyte output mechanism */
#endif
    printf("EUR");                 /* if that fails: ASCII fallback */

  printf("\n");

  return 0;
}
-------------------------------------------------------------------

Compile the above wtest.c and call with

LANG=de_DE ./wtest             -> get ISO 8859-1
LANG=de_DE@euro ./wtest        -> get ISO 8859-15
LANG=de_DE.UTF-8 ./wtest       -> get UTF-8

You'll fully automatically get the encoding that "locale charmap"
reports for the current locale setting.

If you are unfamiliar with C's wide I/O functions, you'll find them
documented in the new ISO C standard

  http://www.cl.cam.ac.uk/~mgk25/volatile/ISO-C-FDIS.1999-04.txt

Markus

-- 
Markus G. Kuhn, Computer Laboratory, University of Cambridge, UK
Email: mkuhn at acm.org,  WWW: <http://www.cl.cam.ac.uk/~mgk25/>

-
Linux-UTF8:   i18n of Linux on all levels
Archive:      http://mail.nl.linux.org/linux-utf8/

Reply via email to