YouTip LogoYouTip

C Standard Library Locale H

C Standard Library – <locale.h>

Introduction

<locale.h> is a header file in the C standard library that supports internationalization and localization of programs. It provides a set of functions and macros to set or query locale information for the program, such as date, time, currency, and numeric formatting.

Next, we will introduce some macros, an important structure struct lconv, and two important functions.

Library Macros

The following table lists the macros defined in the locale.h header file, which will be used in the two functions described below:

Serial No. Macro & Description
1 LC_ALL Used to set or query all locale categories.
2 LC_COLLATE Used to set or query locale information for string comparison.
3 LC_CTYPE Used to set or query locale information for character handling.
4 LC_MONETARY Used to set or query locale information for monetary formatting.
5 LC_NUMERIC Used to set or query locale information for numeric formatting (e.g., decimal point symbol).
6 LC_TIME Used to set or query locale information for time formatting.
7 locale_t Type representing locale information.

Library Functions

The following table lists the functions defined in the locale.h header file:

Serial No. Function & Description
1 char *setlocale(int category, const char *locale) Sets or reads locale information.
2 struct lconv *localeconv(void) Sets or reads locale information.
3 locale_t newlocale(int category_mask, const char *locale, locale_t base) Creates a new locale object.
4 freelocale(locale_t locale) Frees a locale object.
5 locale_t uselocale(locale_t newloc) Sets or queries the locale object for the current thread.

Example

Setting and querying locale information:

Example

#include <stdio.h>

#include <locale.h>

int main(){

// Set locale information to the default setting from user environment variables

setlocale(LC_ALL,"");

// Get and print current locale information

printf("Current locale for LC_ALL: %sn",setlocale(LC_ALL, NULL));

printf("Current locale for LC_TIME: %sn",setlocale(LC_TIME, NULL));

printf("Current locale for LC_NUMERIC: %sn",setlocale(LC_NUMERIC, NULL));

return 0;

}

Compilation output:

Current locale for LC_ALL: zh_CN.UTF-8Current locale for LC_TIME: zh_CN.UTF-8Current locale for LC_NUMERIC: zh_CN.UTF-8

Retrieving numeric and monetary formatting information:

Example

#include <stdio.h>

#include <locale.h>

int main(){

// Set locale information to the default setting from user environment variables

setlocale(LC_ALL,"");

// Get locale-specific numeric and monetary formatting information

struct lconv *lc =localeconv();

// Print numeric and monetary formatting information

printf("Decimal point character: %sn", lc->decimal_point);

printf("Thousands separator: %sn", lc->thousands_sep);

printf("Currency symbol: %sn", lc->currency_symbol);

return 0;

}

Compilation output:

Decimal point character: .Thousands separator: ,Currency symbol: οΏ₯

Using a custom locale object:

Example

#include <stdio.h>

#include <locale.h>

#include <xlocale.h>

int main(){

// Create a new locale object using "en_US.UTF-8" locale

 locale_t newloc = newlocale(LC_ALL_MASK,"en_US.UTF-8",(locale_t)0);

// Set the current thread's locale object to the new locale object

 locale_t oldloc = uselocale(newloc);

// Get and print the current thread's locale information

printf("Current locale for LC_NUMERIC: %sn",setlocale(LC_NUMERIC, NULL));

// Free the new locale object

 uselocale(oldloc);

 freelocale(newloc);

return 0;

}

Compilation output:

Current locale for LC_NUMERIC: C

Library Structure

typedef struct { char *decimal_point; char *thousands_sep; char *grouping; char *int_curr_symbol; char *currency_symbol; char *mon_decimal_point; char *mon_thousands_sep; char *mon_grouping; char *positive_sign; char *negative_sign; char int_frac_digits; char frac_digits; char p_cs_precedes; char p_sep_by_space; char n_cs_precedes; char n_sep_by_space; char p_sign_posn; char n_sign_posn;} lconv

The following table describes each field:

Serial No. Field & Description
1 decimal_point Character used as the decimal point for non-monetary values.
2 thousands_sep Character used as the thousands separator for non-monetary values.
3 grouping A string indicating the size of digit groups in non-monetary quantities. Each character represents an integer value specifying the number of digits in the current group. A value of 0 means the previous value applies to remaining groups.
4 int_curr_symbol String used for international currency symbols. The first three characters are specified by ISO 4217:1987, and the fourth character serves as a separator between the currency symbol and the monetary amount.
5 currency_symbol Local symbol used for currency.
6 mon_decimal_point Character used as the decimal point for monetary values.
7 mon_thousands_sep Character used as the thousands separator for monetary values.
8 mon_grouping A string indicating the size of digit groups in monetary values. Each character represents an integer value specifying the number of digits in the current group. A value of 0 means the previous value applies to remaining groups.
9 positive_sign Character used for positive monetary values.
10 negative_sign Character used for negative monetary values.
11 int_frac_digits Number of digits after the decimal point to display for international monetary values.
12 frac_digits Number of digits after the decimal point to display for monetary values.
13 p_cs_precedes If equal to 1, the currency_symbol appears before the positive monetary value. If equal to 0, the currency_symbol appears after the positive monetary value.
14 p_sep_by_space If equal to 1, a space separates the currency_symbol and the positive monetary value. If equal to 0, no space separates them.
15 n_cs_precedes If equal to 1, the currency_symbol appears before the negative monetary value. If equal to 0, the currency_symbol appears after the negative monetary value.
16 n_sep_by_space If equal to 1, a space separates the currency_symbol and the negative monetary value. If equal to 0, no space separates them.
17 p_sign_posn Indicates the position of the positive sign in positive monetary values.
18 n_sign_posn Indicates the position of the negative sign in negative monetary values.

The following values apply to p_sign_posn and n_sign_posn:

Value Description
0 Parentheses enclosing the value and currency_symbol.
1 Symbol placed before the value and currency_symbol.
2 Symbol placed after the value and currency_symbol.
3 Symbol placed immediately before the value and currency_symbol.
4 Symbol placed immediately after the value and currency_symbol.
← C Standard Library Math HC Standard Library Limits H β†’