Easy Tutorial
❮ C Function Strtok C Function Log10 ❯

C Library Function - setlocale()

C Standard Library - <locale.h>

Description

The C library function char *setlocale(int category, const char *locale) sets or retrieves localization information.

Declaration

Here is the declaration for the setlocale() function.

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

Parameters

Return Value

On successful invocation of setlocale(), it returns an opaque string corresponding to the locale. If the request is invalid, the return value is NULL.

Example

The following example demonstrates the use of the setlocale() function.

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

int main ()
{
   time_t currtime;
   struct tm *timer;
   char buffer[80];

   time( &currtime );
   timer = localtime( &currtime );

   printf("Locale is: %s\n", setlocale(LC_ALL, "en_GB.UTF-8"));
   strftime(buffer,80,"%c", timer );
   printf("Date is: %s\n", buffer);

   printf("Locale is: %s\n", setlocale(LC_ALL, "de_DE.UTF-8"));
   strftime(buffer,80,"%c", timer );
   printf("Date is: %s\n", buffer);

   return(0);
}

Let's compile and run the above program, which will produce the following result:

Locale is: en_GB
Date is: Thu 23 Aug 2012 06:39:32 MST
Locale is: de_DE
Date is: Do 23 Aug 2012 06:39:32 MST

C Standard Library - <locale.h>

❮ C Function Strtok C Function Log10 ❯