C Programming Code Examples
Learn C Language
setlocale() Function in C Programming Language
setlocale() Function in C
Set or retrieve locale. Sets 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 as the value for argument locale.
Locales contain information on how to interpret and perform certain input/output and transformation operations taking into consideration location and language specific settings.
Most running environments have certain locale information set according to the user preferences or localization. But, independently of this system locale, on start, all C programs have the "C" locale set, which is a rather neutral locale with minimal locale information that allows the result of programs to be predictable. In order to use the default locale set in the environment, this function can be called with "" as argument locale.
On program startup, the locale selected is the "C" locale, which is the same as would be set by calling setlocale(LC_ALL,"C").
The locale settings selected in the environment can be selected by calling setlocale(LC_ALL,"").
The portions of the current locale affected by a call to this function are specified by argument category.
Syntax for setlocale() Function in C
#include <locale.h>
char* setlocale (int category, const char* locale);
category
Portion of the locale affected. It is one of the following constant values defined as macros in LC_ALL
Selects all the C locale
LC_COLLATE
Selection the collation category
LC_CTYPE
Selects the character classification category
LC_MONETARY
Selects the monetary formatting category
LC_NUMERIC
Selects the numeric formatting category
LC_TIME
Selects the time formatting category
LC_MESSAGES
specify the language to be used for messages.
locale
C string containing the name of a C locale. These are system specific, but at least the two following locales must exist:
"C"
Minimal "C" locale
""
Environment's default locale
If the value of this parameter is NULL, the function does not make any changes to the current locale, but the name of the current locale is still returned by the function.
On success, function returns a pointer to a C string identifying the locale currently set for the category. If category is LC_ALL and different portions of the locale are set to different values, the string returned gives this information in a format which may vary between library implementations.
If the function failed to set a new locale, this is not modified and a null pointer is returned.
Data races
Changing locale settings may introduce data races with concurrent calls to the same function or to any C-library function affected by the locale.
/* set or retrieve locale by setlocale() function code example */
#include <stdio.h>
#include <locale.h>
int main(int argc, const char * argv[])
{
/* Define a temporary variable */
struct lconv *loc;
/* Set the locale to the POSIX C environment */
setlocale (LC_ALL, "C");
/* Retrieve a pointer to the current locale */
loc = localeconv();
/* Display some of the locale settings */
printf("Thousands Separator: %s\n", loc->thousands_sep);
printf("Currency Symbol: %s\n", loc->currency_symbol);
/* Set the locale to the environment default */
setlocale (LC_ALL, "");
/* Retrieve a pointer to the current locale */
loc = localeconv();
/* Display some of the locale settings */
printf("Thousands Separator: %s\n", loc->thousands_sep);
printf("Currency Symbol: %s\n", loc->currency_symbol);
return 0;
}
Input upper limit to find sum of even number. Store it in a variable say N. Initialize another variable to store sum with 0 say sum = 0. To find sum of even numbers we need to iterate
C Program using recursion displays the first N 'natural number' on the terminal. User enters the Nth number as the input, C program then displays integral number starting from 1 up to
C program using Recursion and finds the first 'capital letter' that exists in a string. We have included 'ctype.h' in order to make use of "int isupper(char); " function that's defined inside
C Program to find and replace any desired character from the input text. Function to find and replace any text. Enter a line of text below. Line will be terminated by pressing...