English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C Language Basic Tutorial

C Language Flow Control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Structure

C Language File

C Others

C Language Reference Manual

C Library Function – Usage and Example of setlocale()

C Standard Library - <locale.h>

Description

C Library Function char *setlocale(int category, const char *locale) Set or read localized information.

Declaration

Below is the declaration of the setlocale() function.

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

Parameter

  • category -- This is a named constant that specifies the function categories affected by the locale.

    • LC_ALL Includes all the options below.

    • LC_COLLATE String comparison. See strcoll().

    • LC_CTYPE Character classification and conversion. For example, strtoupper().

    • LC_MONETARY Currency format, for localeconv().

    • LC_NUMERIC Decimal point separator, for localeconv().

    • LC_TIME Date and time format, for strftime().

    • LC_MESSAGES System Response.

  • locale -- If locale is NULL or an empty string "", the locale name will be set according to the value of the environment variable, with the name the same as the category names above.

Return Value

If setlocale() is called successfully, it returns an opaque string corresponding to the locale setting. If the request is invalid, the return value is NULL.

Online Example

The following example demonstrates the usage 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("The locale is: \t%s\n", setlocale(LC_ALL, "en_GB.UTF"));-8);
   strftime(buffer,80, "%c", timer);
   printf("The date is: \t%s\n", buffer);
 
  
   printf("The locale is: \t%s\n", setlocale(LC_ALL, "de_DE.UTF-8);
   strftime(buffer,80, "%c", timer);
   printf("The date is: \t%s\n", buffer);
 
   return(0);
}

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

Language environment is: en_GB
The date is: Thu 13 Aug 2011 06:39:32 MST
Locale is: de_DE
The date is: Do 13 Aug 2011 06:39:32 MST

C Standard Library - <locale.h>