What is Locale in C?

Question: What is Locale in C?

Answer: In an effort to make “C Language” useful throughout the world, standard defines concept of ‘LOCALE’ which specifies a set of parameters for certain things which vary from country to country. ‘setlocale()’ function is used to determine current locale as well as to change the entire locale or a portion of it. This is defined in ‘locale.h’ header and is prototyped as below

    char *setlocale(int category, char const *locale);

The default is “C” locale and other locales may be defined by the implemnetation. Changing the locale may affect how other library functions work.


If ‘setlocale()’ function is called with second argument as NULL value, function returns default locale for the given ‘category’. For example,

/* locale.c -- determines the curent locale */
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
int main(void)
    char *curr_locale;
    /* find the current locale for different categories */
    curr_locale = setlocale(LC_ALL, NULL);
    printf("Current Locale is \"%s\"\n", curr_locale);
    curr_locale = setlocale(LC_COLLATE, NULL);
    printf("Current Locale is \"%s\"\n", curr_locale);
    curr_locale = setlocale(LC_CTYPE, NULL);
    printf("Current Locale is \"%s\"\n", curr_locale);
    curr_locale = setlocale(LC_MONETARY, NULL);
    printf("Current Locale is \"%s\"\n", curr_locale);
    curr_locale = setlocale(LC_NUMERIC, NULL);
    printf("Current Locale is \"%s\"\n", curr_locale);
    return 0;

Program outputs ‘Current Locale is “C”‘ for all ‘category’ arguments.


If function fails, it returns NULL value and doesn’t modify the current locale. Notice that ‘category’ argument defines which portion of the locale to be modified.

Effects of changing the Locale:

1. Change of locale may add extra characters to character set used when the program is executing. This may not affect the meanings of existing characters.

2. Changes the direction of printing. Change in locale may effect where next character is to be printed in relation to the previous character which was printed.


3. printf and scanf families of functions use decimal character defined by the current locale.

4. Fuctions like ‘isalpha()’, ‘isspace()’, ‘islower()’, ‘isupper() may include more characters than previously defined if locale extends the execution character set.

5. Collating sequence of execution character set may change. This is the sequence used by ‘strcoll()’ to compare strings to each other. Recall that collating sequence of a machine’s character set is fixed. Yet, locale provides a way to specify alternate sequences. In order to specify other than default collating sequence, following two funcions are used.

    int strcoll(char const *string1, char const *string2);
    size_t strxfrm(char *string1, char const *string2, size_t size);

‘strcoll()’ function compares two strings as specified by ‘LC_COLLATE’ category of current locale. This function returns less than, equal to or greater than zero (0) if first string is smaller than, equal to or larger than second string. Though two strings could also be compared using ‘strcmp()’ function but because of the need to obey a collating sequence that isn’t native to machine, ‘strcoll()’ function is used. ‘strxfrm()’ function is provided to reduce the computation required when strings are compared repeatedly in this manner. It converts its second argument, interpreted in the current locale, into another string that isn’t dependent on the locale.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn