C++ Standard Library C++ STL Library

C++ <clocale> - setlocale() Function



The C++ <clocale> setlocale() function sets the locale information to be used by the current program, either changing the entire locale or portions of it. The function can also be used to retrieve the current locale's name by passing NULL pointer as locale argument.

The modifications remain in effect and influences the execution of all locale-sensitive C library functions until the next call to setlocale() function.

Syntax

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

Parameters

category Specify locale category identifier. It is one of the macro constant values defined for locale categories.
locale Specify system-specific locale identifier. These are system specific, but at least the two following locales must exist:
  • "" - Environment's default locale
  • "C" - for the Minimal C locale
If NULL pointer is passed, the function queries the current C locale without modifying it.

Return Value

On success, returns a pointer to a narrow null-terminated string identifying the C locale after applying the changes, if any, or null pointer on failure.

Example:

The example below shows the usage of setlocale() function.

#include <cstdio>
#include <ctime>
#include <clocale>
 
int main (){
  time_t t = time(NULL);
  char buffer [80];

  printf("Local is: %s\n", setlocale(LC_ALL,"en_US.UTF-8"));

  struct lconv *lc = localeconv();

  printf("Local Currency Symbol: %s\n", 
          lc->currency_symbol);

  strftime (buffer,80,"%c",localtime(&t));
  printf("Date is: %s\n", buffer);

  return 0;
}

The output of the above code will be:

Local is: en_US.UTF-8
Local Currency Symbol: $
Date is: Mon 10 May 2021 07:40:29 AM UTC

❮ C++ <clocale> Library