Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

setlocale — select or query locale settings

setlocale selects or queries C library locale settings, which affect behavior including numeric formatting.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <locale.h>
char *setlocale(int category, const char *locale);

setlocale from <locale.h> changes the program's locale. category selects a portion; LC_ALL covers everything. LC_NUMERIC controls, among other things, the decimal-point character.

"C" selects the minimal C environment, "" the native environment; other names are implementation-defined. Programs start in "C". Passing NULL queries without changing settings. A failed change returns NULL, leaving settings unchanged.

Do not modify the returned string. Copy it before another call if needed: subsequently using the returned value is undefined, also after the calling thread exits. Calls can introduce data races with locale-dependent operations.

Examples

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

int main(void)
{
    if (setlocale(LC_NUMERIC, "C") == NULL) {
        return 1;
    }

    printf("%.2f\n", 1234.5);
    return 0;
}

Result

1234.50

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources