summaryrefslogtreecommitdiff
path: root/devdocs/c/locale%2Fsetlocale.html
blob: 23244989d5a28300cdfaae7c2a56ee3d124b7f6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    <h1 id="firstHeading" class="firstHeading">setlocale</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;locale.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td class="t-dcl-nopad"> <pre data-language="c">char* setlocale( int category, const char* locale );</pre>
</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr>  </table> <p>The <code>setlocale</code> 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 <code>setlocale</code>. If <code>locale</code> is a null pointer, <code>setlocale</code> queries the current C locale without modifying it.</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> category </td> <td> - </td> <td> locale category identifier, one of the <a href="lc_categories" title="c/locale/LC categories">LC_xxx</a> macros. May be null. </td>
</tr> <tr class="t-par"> <td> locale </td> <td> - </td> <td> system-specific locale identifier. Can be <code>""</code> for the user-preferred locale or <code>"C"</code> for the minimal locale </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>pointer to a narrow null-terminated string identifying the C locale after applying the changes, if any, or null pointer on failure.</p>
<p>A copy of the returned string along with the category used in this call to <code>setlocale</code> may be used later in the program to restore the locale back to the state at the end of this call.</p>
<h3 id="Notes"> Notes</h3> <p>During program startup, the equivalent of <code>setlocale<span class="br0">(</span><a href="http://en.cppreference.com/w/c/locale/LC_categories"><span class="kw903">LC_ALL</span></a>, <span class="st0">"C"</span><span class="br0">)</span><span class="sy4">;</span></code> is executed before any user code is run.</p>
<p>Although the return type is <code>char*</code>, modifying the pointed-to characters is undefined behavior.</p>
<p>Because <code>setlocale</code> 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: <code><a href="../io/fprintf" title="c/io/fprintf">fprintf</a></code>, <code><a href="../string/byte/isprint" title="c/string/byte/isprint">isprint</a></code>, <code><a href="../string/wide/iswdigit" title="c/string/wide/iswdigit">iswdigit</a></code>, <code><a href="localeconv" title="c/locale/localeconv">localeconv</a></code>, <code><a href="../string/byte/tolower" title="c/string/byte/tolower">tolower</a></code>, <code><a href="../io/fscanf" title="c/io/fscanf">fscanf</a></code>, <code><a href="../string/byte/ispunct" title="c/string/byte/ispunct">ispunct</a></code>, <code><a href="../string/wide/iswgraph" title="c/string/wide/iswgraph">iswgraph</a></code>, <code><a href="../string/multibyte/mblen" title="c/string/multibyte/mblen">mblen</a></code>, <code><a href="../string/byte/toupper" title="c/string/byte/toupper">toupper</a></code>, <code><a href="../string/byte/isalnum" title="c/string/byte/isalnum">isalnum</a></code>, <code><a href="../string/byte/isspace" title="c/string/byte/isspace">isspace</a></code>, <code><a href="../string/wide/iswlower" title="c/string/wide/iswlower">iswlower</a></code>, <code><a href="../string/multibyte/mbstowcs" title="c/string/multibyte/mbstowcs">mbstowcs</a></code>, <code><a href="../string/wide/towlower" title="c/string/wide/towlower">towlower</a></code>, <code><a href="../string/byte/isalpha" title="c/string/byte/isalpha">isalpha</a></code>, <code><a href="../string/byte/isupper" title="c/string/byte/isupper">isupper</a></code>, <code><a href="../string/wide/iswprint" title="c/string/wide/iswprint">iswprint</a></code>, <code><a href="../string/multibyte/mbtowc" title="c/string/multibyte/mbtowc">mbtowc</a></code>, <code><a href="../string/wide/towupper" title="c/string/wide/towupper">towupper</a></code>, <code><a href="../string/byte/isblank" title="c/string/byte/isblank">isblank</a></code>, <code><a href="../string/wide/iswalnum" title="c/string/wide/iswalnum">iswalnum</a></code>, <code><a href="../string/wide/iswpunct" title="c/string/wide/iswpunct">iswpunct</a></code>, <code>setlocale</code>, <code><a href="../string/wide/wcscoll" title="c/string/wide/wcscoll">wcscoll</a></code>, <code><a href="../string/byte/iscntrl" title="c/string/byte/iscntrl">iscntrl</a></code>, <code><a href="../string/wide/iswalpha" title="c/string/wide/iswalpha">iswalpha</a></code>, <code><a href="../string/wide/iswspace" title="c/string/wide/iswspace">iswspace</a></code>, <code><a href="../string/byte/strcoll" title="c/string/byte/strcoll">strcoll</a></code>, <code><a href="../string/wide/wcstof" title="c/string/wide/wcstof">wcstod</a></code>, <code><a href="../string/byte/isdigit" title="c/string/byte/isdigit">isdigit</a></code>, <code><a href="../string/wide/iswblank" title="c/string/wide/iswblank">iswblank</a></code>, <code><a href="../string/wide/iswupper" title="c/string/wide/iswupper">iswupper</a></code>, <code><a href="../string/byte/strerror" title="c/string/byte/strerror">strerror</a></code>, <code><a href="../string/multibyte/wcstombs" title="c/string/multibyte/wcstombs">wcstombs</a></code>, <code><a href="../string/byte/isgraph" title="c/string/byte/isgraph">isgraph</a></code>, <code><a href="../string/wide/iswcntrl" title="c/string/wide/iswcntrl">iswcntrl</a></code>, <code><a href="../string/wide/iswxdigit" title="c/string/wide/iswxdigit">iswxdigit</a></code>, <code><a href="../string/byte/strtof" title="c/string/byte/strtof">strtod</a></code>, <code><a href="../string/wide/wcsxfrm" title="c/string/wide/wcsxfrm">wcsxfrm</a></code>, <code><a href="../string/byte/islower" title="c/string/byte/islower">islower</a></code>, <code><a href="../string/wide/iswctype" title="c/string/wide/iswctype">iswctype</a></code>, <code><a href="../string/byte/isxdigit" title="c/string/byte/isxdigit">isxdigit</a></code>.</p>
<p>POSIX also defines a locale named "POSIX", which is always accessible and is exactly equivalent to the default minimal "C" locale.</p>
<p>POSIX also specifies that the returned pointer, not just the contents of the pointed-to string, may be invalidated by subsequent calls to <code>setlocale</code>.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
#include &lt;locale.h&gt;
#include &lt;time.h&gt;
#include &lt;wchar.h&gt;
 
int main(void)
{
    // the C locale will be UTF-8 enabled English;
    // decimal dot will be German
    // date and time formatting will be Japanese
    setlocale(LC_ALL, "en_US.UTF-8");
    setlocale(LC_NUMERIC, "de_DE.utf8");
    setlocale(LC_TIME, "ja_JP.utf8");
 
    wchar_t str[100];
    time_t t = time(NULL);
    wcsftime(str, 100, L"%A %c", localtime(&amp;t));
    wprintf(L"Number: %.2f\nDate: %ls\n", 3.14, str);
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">Number: 3,14
Date: 月曜日 2017年09月25日 13時00分15秒</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 7.11.1.1 The setlocale function (p: 163-164) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.11.1.1 The setlocale function (p: 224-225) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.11.1.1 The setlocale function (p: 205-206) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.4.1.1 The setlocale function </li></ul>
</ul>               <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="lc_categories" title="c/locale/LC categories"> <span class="t-lines"><span>LC_ALL</span><span>LC_COLLATE</span><span>LC_CTYPE</span><span>LC_MONETARY</span><span>LC_NUMERIC</span><span>LC_TIME</span></span></a></div> </td> <td> locale categories for <code>setlocale</code> <br> <span class="t-mark">(macro constant)</span>  </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/locale/setlocale" title="cpp/locale/setlocale">C++ documentation</a></span> for <code>setlocale</code> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
    <a href="https://en.cppreference.com/w/c/locale/setlocale" class="_attribution-link">https://en.cppreference.com/w/c/locale/setlocale</a>
  </p>
</div>