This is the mail archive of the cygwin mailing list for the Cygwin project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: gcc4[1.7] printf treats differently a string constant and a character array

2009/12/29 Rodrigo Medina:
>>Ah, the problem actually is that your program is missing a call to
>>setlocale(LC_CTYPE, "") to switch to the locale and character set
>>specified in the environment...
> That worked!, but what that means is that if one wants to
> use any locale other than C.UTF-8, one has, not only to compile again the
> programs , but also to modify them. Perhaps the best thing to do
> is to read the LC_ALL variable from the environment and then call
> setlocale.

setlocale(LC_CTYPE, "") already does that. It tries to read LC_ALL,
LC_CTYPE, and LANG, in that order, and only if none of them are set it
falls back to the default locale: C.UTF-8.

'char' string constants with non-ASCII characters are not a good idea
if the program is supposed to work with different charsets, because
they're encoded in one particular charset, namely that of your editor.

In that case you need to use wchar_t strings instead, for example:

#include <locale.h>
#include <wchar.h>

int main(void) {
  setlocale(LC_CTYPE, "");

You also need to ensure that gcc's character set matches the source
file's; otherwise gcc won't encode the wide string correctly.


Problem reports:
Unsubscribe info:

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]