C Standard Library |
---|
|
In computing, C localization functions are a group of functions in the C programming language implementing basic localization routines.[1][2] The functions are used in multilingual programs to adapt to the specific locale. In particular, the way of displaying of numbers and currency can be modified. These settings affect the behaviour of input/output functions in the C Standard Library.
Contents |
C localization functions and types are defined in locale.h (clocale header in C++).
struct lconv
char* decimal point;
char* grouping;
struct lconv* localeconv(void);
char* setlocale(int, const char*);
charthousand sep;
char* currency symbol;
char* int curr symbol;
char*mon-decimal point
char* mon grouping;
char* mon thousand sep;
C standard localization functions are criticized because the localization state is stored globally. This means that in a given program all operations involving a locale can use only one locale at a time. As a result, it is very difficult to implement programs that use more than one locale.[4]
The functions alter the behavior of printf/scanf/strtod which are often used to write saved data to a file or to other programs. The result is that a saved file in one locale will not be readable in another locale, or not be readable at all due to assumptions such as "numbers end at comma characters". Most large-scale software forces the locale to "C" (or another fixed value) to work around these problems.
Another criticism is that these functions do not address at all the far more important problem of translating text to a different language. A solution that could substitute text strings could easily substitute printf format strings, if some extra %-sequences were added to control the location of commas this could achieve all the number formatting fixes in a far more obvious and easily-controlled way.
#include <stdio.h> #include <stdlib.h> #include <locale.h> int main(void) {/* Locale is set to "C" before this. This call sets it to the "current locale" by reading environment variables: */ setlocale(LC_ALL, ""); const struct lconv * const currentlocale = localeconv(); printf("In the current locale, the default currency symbol is: %s\n", currentlocale->currency_symbol); return EXIT_SUCCESS;}