Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/locale/setlocale"

From cppreference.com
< cpp‎ | locale
m (typo)
m (Text replace - "{{example cpp" to "{{example")
Line 27: Line 27:
  
 
===Example===
 
===Example===
{{example cpp
+
{{example
 
  |  
 
  |  
 
  | code=
 
  | code=

Revision as of 16:51, 19 April 2012

Template:cpp/locale/sidebar Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <clocale>
</td>

<td></td> <td></td> </tr> <tr class="t-dcl ">

<td class="t-dcl-nopad">
char* setlocale( int category, const char* locale);
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

The setlocale function installs the specified system locale or its portion as the new C locale. The modifications remain in effect and influences the execution of all locale-sensitive C library functions until the next call to setlocale. If locale is a null pointer, setlocale queries the current C locale without modifying it.

Contents

Parameters

category - locale category identifier, one of the LC_xxx macros. May be null.
locale - system-specific locale identifier. Can be Template:cpp for the user-preferred locale or Template:cpp for the minimal locale

Return value

pointer to a narrow null-terminated string identifying the C locale after applying the changes, if any, or null pointer on failure.

Notes

During program startup, the equivalent of Template:cpp is executed before any user code is run.

Although the return type is Template:cpp, modifying the pointed-to characters is undefined behavior.

Because setlocale modifies global state which affects execution of locale-dependent functions, it is undefined behavior to call it from one thread, while another thread is executing any of the following functions: Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp, Template:cpp.

Example

#include <cstdio>
#include <clocale>
#include <ctime>
#include <cwchar>
int main()
{
    std::setlocale(LC_ALL, "en_US.UTF-8"); // the C locale will be the UTF-8 enabled English
    std::setlocale(LC_NUMERIC, "de_DE");   // decimal dot will be German
    std::setlocale(LC_TIME, "ja_JP");      // date/time formatting will be Japanese
    wchar_t str[100];
    std::time_t t = std::time(NULL);
    std::wcsftime(str, 100, L"%A %c", std::localtime(&t));
    std::wprintf(L"Number: %.2f\nDate: %Ls\n", 3.14, str);
}

Output:

Number: 3,14
Date: 月曜日 2011年12月19日 18時04分40秒

See also

Template:cpp/locale/dcl list LC categoriesTemplate:cpp/locale/dcl list locale