C Standard Library

C <locale.h> - setlocale() Function



The C <locale.h> 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 <stdio.h>
#include <time.h>
#include <locale.h>
 
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: Wed 07 Apr 2021 08:53:11 AM UTC

❮ C <locale.h> Library