C locale.h Header

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

What Is locale.h?

<locale.h> is a C standard header for localization. Use setlocale to set or query cultural conventions, and localeconv to read details like the decimal-point character.

Remember
#include <locale.h>

setlocale(category, name)
  name = NULL  → query only
  name = "C"   → portable default
  name = ""    → use LANG / LC_* from the environment

localeconv() → struct lconv
  decimal_point, thousands_sep, ...

programs start in the "C" locale
setlocale is NOT thread-safe
UsePrefer
Portable number parsingsetlocale(LC_NUMERIC, "C")
User-facing dates / formatssetlocale(LC_ALL, "") + strftime
Read decimal / currency symbolslocaleconv()

How to Use locale.h in C

Pick a category, set or query the locale, then use locale-aware library calls.

  1. Add #include <locale.h> at the top of the file.
  2. Call setlocale(LC_ALL, "") early if you want the user’s environment.
  3. Use setlocale(LC_NUMERIC, "C") when parsing config numbers that always use ..
  4. Call localeconv() when you need decimal_point or related fields.
PieceRequired?Role
#include <locale.h>YesDeclares setlocale and localeconv
Category (LC_*)YesWhich rules to change (LC_ALL, LC_NUMERIC, …)
Locale nameYes*"C", "", a system name, or NULL to query (*query uses NULL)

Pseudocode

Pseudocode
include locale.h

// start: locale is "C"

if need user formats:
    setlocale(LC_ALL, "")     // from environment
    if failed: keep "C" or exit

if need portable parse:
    setlocale(LC_NUMERIC, "C")
    value = strtod(text, ...)

info = localeconv()
use info.decimal_point for display helpers

Cheat Sheet

Keep these categories and calls handy while localizing a program.

TopicDetail
Header#include <locale.h>
Set / querysetlocale(category, name)
LC_ALLAll categories at once
LC_NUMERICDecimal point; affects printf / strtod
LC_TIMEDate/time names for strftime
LC_CTYPECharacter classes (ctype.h)
"C"Portable default (dot decimal)
""User locale from the environment

Live Preview

Simulate common setlocale choices and what localeconv reports for the decimal point.

Browser simulation of locale ideas (same concepts as the C examples below).

Result
setlocale(LC_ALL, NULL) → "C"

Programs start here.
decimal_point = "."
strtod("3.14") works with a dot.

Worked Walkthrough — Parse Then Display

Keep parsing in the C numeric locale, then switch to the user locale for display-friendly rules.

Calculation
Start: LC_ALL is "C"

1) setlocale(LC_NUMERIC, "C")
   text = "3.14"
   strtod(text) → 3.14     // always expects '.'

2) setlocale(LC_ALL, "")
   // LANG / LC_* from environment
   localeconv()->decimal_point
     may be "." or "," depending on region

3) User sees local conventions;
   config files still parsed with C numeric ✓

C Examples

Compile with gcc file.c -std=c11 -o out. Locale names and localeconv fields depend on the OS and environment variables.

Example 1 — Query and Set the Locale

Programs start in the C locale. Query with NULL, then set "C" explicitly.

C
#include <locale.h>
#include <stdio.h>

int main(void) {
    printf("At start: %s\n", setlocale(LC_ALL, NULL));

    if (setlocale(LC_ALL, "C") == NULL) {
        printf("Failed to set C locale\n");
        return 1;
    }
    printf("After C:  %s\n", setlocale(LC_ALL, NULL));

    return 0;
}

How It Works

  1. Query only. setlocale(LC_ALL, NULL) returns the current name without changing it.
  2. Default is C. Before any call, the process uses the portable C locale.
  3. Set explicitly. setlocale(LC_ALL, "C") returns the new name or NULL on failure.
  4. Check the result. Always test for NULL when changing locale.

Example 2 — Read localeconv Fields

After selecting a locale, localeconv exposes the decimal point and thousands separator.

C
#include <locale.h>
#include <stdio.h>

int main(void) {
    setlocale(LC_ALL, "C");

    struct lconv *lc = localeconv();
    printf("decimal_point: '%s'\n", lc->decimal_point);
    printf("thousands_sep: '%s'\n", lc->thousands_sep);
    printf("currency_symbol: '%s'\n", lc->currency_symbol);

    return 0;
}

How It Works

  1. Stay in C. Guarantees a predictable sample on every machine.
  2. Call localeconv. Returns a pointer to a static struct lconv.
  3. Read string fields. In the C locale, decimal_point is "."; others are often empty.
  4. Try user locale later. setlocale(LC_ALL, "") may change these strings to match the region.

Example 3 — Parse Numbers in the C Locale

Force LC_NUMERIC to "C" so strtod always accepts a dot decimal, even if the rest of the program uses the user locale.

C
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    setlocale(LC_ALL, "");           /* user locale for the app */
    setlocale(LC_NUMERIC, "C");      /* portable number parsing */

    double value = strtod("3.14", NULL);
    printf("Parsed: %g\n", value);
    printf("LC_NUMERIC: %s\n", setlocale(LC_NUMERIC, NULL));

    return 0;
}

How It Works

  1. Optional user locale. LC_ALL, "" loads environment conventions for display.
  2. Override numeric. LC_NUMERIC, "C" keeps a dot decimal for parsing.
  3. Parse safely. strtod("3.14", NULL) succeeds regardless of regional commas.
  4. Query to confirm. setlocale(LC_NUMERIC, NULL) shows the active numeric locale.

Edge Cases & Pitfalls

Check these before changing locale in real programs.

threads

setlocale is not thread-safe

It updates global process state. Do not change locale while other threads call locale-sensitive functions.

pointer

Copy names before another setlocale

The string from setlocale(..., NULL) may point to a static buffer. Copy it if you must restore later.

names

Locale names are not portable

"en_US.UTF-8" may fail on Windows. Prefer "C" and "", or detect available names.

parse

User locale can break config parsing

After LC_ALL, "", strtod may expect a comma. Keep LC_NUMERIC as "C" for machine-readable input.

Performance Notes

ApproachCostBest for
setlocale once at startupOne-time setupMost interactive apps
Frequent setlocaleExpensive + not thread-safeAvoid in hot paths
localeconvCheap pointer returnReading format symbols

Call setlocale early and rarely. Prefer category-specific changes (LC_NUMERIC) over flipping LC_ALL in loops.

Key Takeaways

  • Role: setlocale + localeconv for cultural conventions.
  • Defaults: start in "C"; use "" for the user environment.
  • Parsing: keep LC_NUMERIC as "C" for config files.
  • Safety: not thread-safe—set early, avoid mid-flight changes.

One line: locale.h lets you choose portable C rules or the user’s region—pick deliberately per task.

Frequently Asked Questions

locale.h provides setlocale() to change or query cultural conventions (numbers, dates, collation, character classes) and localeconv() to read numeric and monetary formatting details such as decimal_point.
It sets the entire locale from environment variables such as LANG and LC_*. An empty string means use the user's environment. On success it returns a locale name; on failure it returns NULL.
The default portable locale, selected with setlocale(LC_ALL, "C"). Numbers use a dot as the decimal point and behavior is predictable across platforms. Programs start in the C locale unless changed.
No. setlocale changes global locale state. Avoid changing locale in one thread while others call locale-sensitive functions, or use platform-specific per-thread locale APIs.
localeconv() returns a pointer to struct lconv with fields like decimal_point, thousands_sep, and currency_symbol for the current locale. The data is static—copy strings if you need them after another locale change.
Yes for LC_NUMERIC. In non-C locales the decimal separator may be a comma. For portable config parsing, use the C locale (or LC_NUMERIC "C") before strtod.

Next: math.h

Use trig, roots, and power functions from the math library.

math.h tutorial →

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