1
votes

In linux terminal one would type

locale charmap

in order to see what kind of character-encoding your system uses, eg UTF-8. My question is how would you do this using c/c++. (I'm using linux)

edit: I tried using

nl_langinfo(CODESET)

but I got ANSI_X3.4-1968 instead of UTF-8 (which is what I get when typing: locale charmap). Am I using nl_langinfo() wrong?

3

3 Answers

4
votes
SETLOCALE(3)               Linux Programmer’s Manual              SETLOCALE(3)

NAME
       setlocale - set the current locale

SYNOPSIS
       #include <locale.h>

       char *setlocale(int category, const char *locale);

DESCRIPTION
       The  setlocale() function is used to set or query the program’s current
       locale.

NL_LANGINFO(3)             Linux Programmer’s Manual            NL_LANGINFO(3)

NAME
       nl_langinfo - query language and locale information

SYNOPSIS
       #include <langinfo.h>

       char *nl_langinfo(nl_item item);

DESCRIPTION
       The  nl_langinfo()  function provides access to locale information in a
       more flexible way than localeconv(3) does.  Individual  and  additional
       elements  of  the locale categories can be queried.  setlocale(3) needs
       to be executed with proper arguments before.
       Examples for the locale elements that can be specified  in  item  using
       the constants defined in <langinfo.h> are:

       CODESET (LC_CTYPE)
          Return  a string with the name of the character encoding used in
          the  selected  locale,  such  as   "UTF-8",   "ISO-8859-1",   or
          "ANSI_X3.4-1968"  (better  known as US-ASCII).  This is the same
          string that you get with "locale charmap".  For a list of  char‐
          acter encoding names, try "locale -m", cf. locale(1).
1
votes

You have to call first setlocale() to populate info about current system locale into your program, becasue by default locale is "C". After that, you will be able to get system locale.

Here is example

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

int main()
{
        setlocale(LC_ALL, "");
        char* locstr = setlocale(LC_CTYPE, NULL);
        char* encoding = nl_langinfo(CODESET);
        printf("Locale is %s\n", locstr);
        printf("Encoding is %s\n", encoding);
        return 0;
}
0
votes

Usually, you don't do so explicitly. Instead, you set your program to use a locale named "". That will give you whatever locale the system is set to use.