Home C locale.h Functions setlocale() C setlocale() Function Definition In C, setlocale() sets or queries the program’s locale. It is declared in <locale.h> and controls cultural rules for numbers, dates, collation, and related behavior.
Pass a category such as LC_ALL or LC_NUMERIC, plus a locale name. Use "C" for the portable default, "" for the environment locale, or NULL to query without changing.
Syntax Pass the category to change and the locale name to apply.
Call What it does setlocale(LC_ALL, "C");Sets every locale category to the portable C locale.
Include <locale.h>. After changing the locale, localeconv() can read numeric formatting fields.
Library declarations char *setlocale(int category, const char *locale); Parameters Parameter Description category Required. Which rules to change, such as LC_ALL, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_CTYPE, or LC_MONETARY. locale Required. Locale name string, "" for the environment locale, "C" for the portable locale, or NULL to query only.
Return Value Return Value Description char * On success, a pointer to a string naming the new (or current) locale setting. NULL The request failed. The previous locale remains in effect.
C Examples 1. Query current locale 2. Force C locale 3. Change LC_NUMERIC only Example 1 — Query the Current Locale Name Pass NULL as the locale name to read the current setting without changing it.
#include <stdio.h>
#include <locale.h>
int main(void) {
char *name = setlocale(LC_ALL, NULL);
if (name == NULL) {
printf("query failed\n");
return 1;
}
printf("current = %s\n", name);
return 0;
} How It Works Pass NULL. setlocale(LC_ALL, NULL) only queries.Check the pointer. A NULL result means the query failed.Print the name. Programs usually start in the C locale, so the name is often C.No change made. Because the second argument was NULL, the locale stays the same.Example 2 — Force the Portable C Locale Set LC_ALL to "C" so number formatting stays predictable across systems.
#include <stdio.h>
#include <locale.h>
int main(void) {
if (setlocale(LC_ALL, "C") == NULL) {
printf("could not set C locale\n");
return 1;
}
printf("locale = %s\n", setlocale(LC_ALL, NULL));
printf("number = %.2f\n", 12.5);
return 0;
} locale = C
number = 12.50 How It Works Request the C locale. setlocale(LC_ALL, "C") selects portable defaults.Handle failure. If the call returns NULL, stop before assuming the change worked.Confirm with a query. A second call with NULL prints the active name.Print a sample number. In the C locale, printf uses a dot decimal point.Example 3 — Change Only LC_NUMERIC Adjust numeric formatting without touching every category. Exact names depend on the system.
#include <stdio.h>
#include <locale.h>
int main(void) {
char *before = setlocale(LC_NUMERIC, NULL);
printf("before = %s\n", before ? before : "(null)");
if (setlocale(LC_NUMERIC, "C") == NULL) {
printf("LC_NUMERIC change failed\n");
return 1;
}
printf("after = %s\n", setlocale(LC_NUMERIC, NULL));
return 0;
} How It Works Query one category. LC_NUMERIC controls number formatting rules.Set only that category. Other categories such as LC_TIME stay unchanged.Check for NULL. Unsupported locale names fail and leave the old setting.Query again. Confirm the numeric category name after the change.Frequently Asked Questions What does setlocale() do in C? It changes or queries the program locale for categories such as numbers, time, and collation.
What does setlocale(LC_ALL, "") do? It sets all categories from the environment (LANG / LC_*). On success it returns a locale name; on failure it returns NULL.
What is the C locale? The portable default selected with "C". Programs start there unless you change it.
How do I query without changing? Call setlocale(category, NULL).
Is setlocale thread-safe? No. It updates global locale state. Avoid changing it while other threads use locale-sensitive functions.
Next: C Locales Review the full beginner model for categories, C vs environment locales, and localeconv.
Open overview → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion