[RFC] Another idea for 6015

Paolo Carlini pcarlini@unitus.it
Thu Apr 4 12:49:00 GMT 2002


Benjamin Kosnik wrote:

>> int main()
>> {
>> setlocale(LC_ALL, "it_IT");
>>
>> printf("%g\n", 30.34);
>>
>> return 0;
>> }
>
> You have to set LANG, not LC_ALL. Again, see the testsuite entries in
> 22_locale.

Indeed, whatever I will post will be /carefully/ tested with the testsuite!

However, in my understanding of the C locales in order to change the 
behaviour of the *printf functions what really counts is issuing a 
setlocale, perhaps after a setenv. Like this:

#include <locale>
#include <cstdio>

using namespace std;

int main()
{
  int a = setenv("LANG", "it_IT", 1);

  printf("%d\n", a);  // a == 0

  setlocale(LC_ALL, "");

  printf("%f\n", 30.50);
}

without the setlocale call, simply the "C"-locale is used in the printf. 
In the glibc2.2.5 docs I found this:

------------  

  A C program inherits its locale environment variables when it starts
up.  This happens automatically.  However, these variables do not
automatically control the locale used by the library functions, because
ISO C says that all programs start by default in the standard `C'
locale.  To use the locales specified by the environment, you must call
`setlocale'.  Call it as follows:

     setlocale (LC_ALL, "");

to select a locale based on the user choice of the appropriate
environment variables.

   You can also use `setlocale' to specify a particular locale, for
general use or for a specific category.
------------

Therefore, I think my idea passes this kind of test ;-)
Right?

Ciao, P.



More information about the Libstdc++ mailing list