Latest web development tutorials

C library functions - setlocale ()

C standard library - <locale.h> C standard library - <locale.h>

description

C library functionschar * setlocale (int category, const char * locale) to set or read local information.

statement

The following is a statement setlocale () function.

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

parameter

  • category - This is a named constant specifying the category of functions that affect the locale.
    • LC_ALL includes all options below.
    • CompareLC_COLLATE string.See strcoll ().
    • LC_CTYPE character classification and conversion.For example strtoupper ().
    • LC_MONETARY currency format for localeconv ().
    • LC_NUMERIC decimal separator for localeconv ().
    • LC_TIME date and time format for strftime ().
    • LC_MESSAGES system response.
  • locale - If locale is NULL or the empty string "", the zone will be set according to the name of the environment variable value, which is the name of the above categories the same name.

return value

If successful call to setlocale (), it returns a corresponding locale opaque string. If the request is invalid, the return value is NULL.

Examples

The following example demonstrates the setlocale () function is used.

#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"));
   strftime(buffer,80,"%c", timer );
   printf("Date is: %s\n", buffer);

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

   return(0);
}

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

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 standard library - <locale.h>