Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Function - setlocale()

C Library Function - setlocale()

C Standard Library - <locale.h> C Standard Library -

Description

setlocale is a function in the C standard library a function in the C standard library used to set or query a program's locale information. It allows programmers to specify the locale used for character classification, character conversion, currency formatting, date and time formatting, number formatting, and so on.

Declaration

The following is the declaration of the setlocale() function.



#include 



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

Parameters

- `LC_ALL`: Sets or queries all locale categories. - `LC_COLLATE`: Sets or queries locale information for string comparison. - `LC_CTYPE`: Sets or queries locale information for character handling. - `LC_MONETARY`: Sets or queries locale information for currency formatting. - `LC_NUMERIC`: Sets or queries locale information for number formatting. - `LC_TIME`: Sets or queries locale information for time formatting. - `""`: Sets to the default from the user's environment variables. - `NULL`: Queries the current locale information. - `specific locale name`: Such as "en_US.UTF-8", used to set a specific locale.

Return Value

Example

The following is sample code using setlocale that shows how to set and query locale information.

Setting and querying locale information:

Example

#include

#include

int

main

(

)

{

   

// Query the current locale information

   

printf

(

"Default locale: %s\n"

,

setlocale

(

LC_ALL

,

NULL

)

)

;

   

// Set the localization information to the default settings from the user environment variables

   

setlocale

(

LC_ALL

,

""

)

;

   

// Get and print the current locale information

   

printf

(

"Locale after setting to '': %s\n"

,

setlocale

(

LC_ALL

,

NULL

)

)

;

   

// Set the locale information to "en_US.UTF-8"

   

if

(

setlocale

(

LC_ALL

,

"en_US.UTF-8"

)

!=

NULL

)

{

       

printf

(

"Locale after setting to 'en_US.UTF-8': %s\n"

,

setlocale

(

LC_ALL

,

NULL

)

)

;

   

}

else

{

       

printf

(

"Unable to set locale to 'en_US.UTF-8'\n"

)

;

   

}

   

return

0

;

}

Let's compile and run the above program, which will produce the following result:



Default locale: C

Locale after setting to '': zh_CN.UTF-8

Locale after setting to 'en_US.UTF-8': en_US.UTF-8

Formatting numbers according to locale information:

Example

#include

#include

int

main

(

)

{

   

// Set the localization information to the default settings from the user environment variables

   

setlocale

(

LC_NUMERIC

,

""

)

;

   

// Print the localized floating-point number

   

printf

(

"Formatted number: %'.2f\n"

,

1234567.89

)

;

   

// Set the locale information to "de_DE.UTF-8" (German - Germany)

   

if

(

setlocale

(

LC_NUMERIC

,

"de_DE.UTF-8"

)

!=

NULL

)

{

       

printf

(

"Locale after setting to 'de_DE.UTF-8': %s\n"

,

setlocale

(

LC_NUMERIC

,

NULL

)

)

;

       

printf

(

"Formatted number in German locale: %'.2f\n"

,

1234567.89

)

;

   

}

else

{

       

printf

(

"Unable to set locale to 'de_DE.UTF-8'\n"

)

;

   

}

   

return

0

;

}

Let's compile and run the above program, which will produce the following result:



Formatted number: 1,234,567.89

Locale after setting to 'de_DE.UTF-8': de_DE.UTF-8

Formatted number in German locale: 1234567,89

Detailed explanation

  1. Setting and querying locale information
  1. Locale categories

Each category controls a specific aspect of the program:

Summary

setlocale The function is an important tool for setting and querying a program's locale information. It allows a program to adapt to different languages and cultural conventions, thereby achieving internationalization and localization. By using setlocale, a program can correctly handle and display localized information such as strings, numbers, currency, dates, and times.

C Standard Library - <locale.h> C Standard Library -