C setlocale() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
locale.h

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.

C
setlocale(LC_ALL, "C");
CallWhat 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
C
char *setlocale(int category, const char *locale);

Parameters

ParameterDescription
categoryRequired. Which rules to change, such as LC_ALL, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_CTYPE, or LC_MONETARY.
localeRequired. Locale name string, "" for the environment locale, "C" for the portable locale, or NULL to query only.

Return Value

Return ValueDescription
char *On success, a pointer to a string naming the new (or current) locale setting.
NULLThe request failed. The previous locale remains in effect.

C Examples

Example 1 — Query the Current Locale Name

Pass NULL as the locale name to read the current setting without changing it.

C
#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

  1. Pass NULL. setlocale(LC_ALL, NULL) only queries.
  2. Check the pointer. A NULL result means the query failed.
  3. Print the name. Programs usually start in the C locale, so the name is often C.
  4. 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.

C
#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;
}

How It Works

  1. Request the C locale. setlocale(LC_ALL, "C") selects portable defaults.
  2. Handle failure. If the call returns NULL, stop before assuming the change worked.
  3. Confirm with a query. A second call with NULL prints the active name.
  4. 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.

C
#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

  1. Query one category. LC_NUMERIC controls number formatting rules.
  2. Set only that category. Other categories such as LC_TIME stay unchanged.
  3. Check for NULL. Unsupported locale names fail and leave the old setting.
  4. Query again. Confirm the numeric category name after the change.

Frequently Asked Questions

It changes or queries the program locale for categories such as numbers, time, and collation.
It sets all categories from the environment (LANG / LC_*). On success it returns a locale name; on failure it returns NULL.
The portable default selected with "C". Programs start there unless you change it.
Call setlocale(category, NULL).
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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