
- C Library - Home
- C Library - <assert.h>
- C Library - <complex.h>
- C Library - <ctype.h>
- C Library - <errno.h>
- C Library - <fenv.h>
- C Library - <float.h>
- C Library - <inttypes.h>
- C Library - <iso646.h>
- C Library - <limits.h>
- C Library - <locale.h>
- C Library - <math.h>
- C Library - <setjmp.h>
- C Library - <signal.h>
- C Library - <stdalign.h>
- C Library - <stdarg.h>
- C Library - <stdbool.h>
- C Library - <stddef.h>
- C Library - <stdio.h>
- C Library - <stdlib.h>
- C Library - <string.h>
- C Library - <tgmath.h>
- C Library - <time.h>
- C Library - <wctype.h>
- C Programming Resources
- C Programming - Tutorial
- C - Useful Resources
C library - setlocale() function
The C library setlocale() function sets or reads location dependent information. This function install the system locale in specific system. To modify the remaining effect and influences, it executes all the sensitive function of C STL before the next call to setlocale.
Syntax
Following is the syntax of the C setlocale() function −
char *setlocale(int category, const char *locale)
Parameters
Below is the list of different locale categories −
-
category − This is a named constant specifying the category of the functions affected by the locale setting.
- LC_ALL for all of the below.
- LC_COLLATE for string comparison. See strcoll().
- LC_CTYPE for character classification and conversion. For example − strtoupper().
- LC_MONETARY for monetary formatting for localeconv().
- LC_NUMERIC for decimal separator for localeconv().
- LC_TIME for date and time formatting with strftime().
- LC_MESSAGES for system responses.
- locale − If locale is NULL or the empty string(""), the locale names will be set from the values of environment variables with the same names as the above categories.
Return Value
A successful call to setlocale() returns an opaque string that corresponds to the locale set. The return value is NULL if the request cannot be honored.
Example 1
Following is the basic C program to demonstrate the usage of 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")); 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); }
Output
After executing the code, we get the following result −
Locale is: en_GB Date is: Fri 05 Dec 2014 10:35:02 UTC Locale is: de_DE Date is: Fr 05 Dez 2014 10:35:02 UTC
Example 2
In this example, we illustrate the use of the decimal separator with the LC_Numeric locale by utilizing the setlocale() function.
#include <stdio.h> #include <locale.h> int main() { // Set the locale to the user's default locale setlocale(LC_NUMERIC, ""); // Get the current locale for LC_NUMERIC char *currentLocale = setlocale(LC_NUMERIC, NULL); printf("Current LC_NUMERIC locale: %s\n", currentLocale); // Display the floating-point number with the current locale double number = 65743.6789; printf("Formatted number: %.2f\n", number); return 0; }
Output
On execution of above code, we get the following result −
Current LC_NUMERIC locale: en_US.UTF-8 Formatted number: 65743.68
Example 3
In this example, we utilize the function setlocale() to set the LC_TIME(local) that display the date and time formatting.
#include <stdio.h> #include <time.h> #include <locale.h> int main() { // Set the locale to the user's default locale setlocale(LC_TIME, ""); // Get the current locale for LC_TIME char *currentLocale = setlocale(LC_TIME, NULL); printf("Current LC_TIME locale: %s\n", currentLocale); // Display the current date and time time_t currentTime; struct tm *timeInfo; time(¤tTime); timeInfo = localtime(¤tTime); char formattedTime[100]; strftime(formattedTime, sizeof(formattedTime), "%c", timeInfo); printf("Formatted date and time: %s\n", formattedTime); return 0; }
Output
The above code produces the following result −
Current LC_TIME locale: en_US.UTF-8 Formatted date and time: Mon 20 May 2024 05:48:37 AM UTC