首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::setlocale

Defined in header <clocale>

?

?

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

?

?

setlocale函数将指定的系统区域设置或其部分安装为新的C语言环境。这些修改仍然有效,并影响所有对地区敏感的C库函数的执行,直到下一次调用setlocale.如果locale为空指针,setlocale查询当前C语言环境而不修改它。

参数

category

-

locale category identifier, one of the LC_xxx macros. May be 0.

locale

-

system-specific locale identifier. Can be "" for the user-preferred locale or "C" for the minimal locale

返回值

指向一个窄的以空结尾的字符串的指针,该字符串在应用更改(如果有的话)之后标识C语言环境,或者在失败时使用空指针。

返回的字符串的副本以及在此调用中使用的类别。std::setlocale可能会在程序后面使用,以便在此调用结束时将区域设置恢复到状态。

注记

在程序启动期间,相当于std::setlocale(LC_ALL, "C");在运行任何用户代码之前执行。

尽管返回类型是char*修改指向字符是未定义的行为。

因为setlocale修改影响区域设置相关函数执行的全局状态,从一个线程调用它是未定义的行为,而另一个线程正在执行下列任何函数:std::fprintf,,,std::isprint,,,std::iswdigit,,,std::localeconv,,,std::tolower,,,std::fscanf,,,std::ispunct,,,std::iswgraph,,,std::mblen,,,std::toupper,,,std::isalnum,,,std::isspace,,,std::iswlower,,,std::mbstowcs,,,std::towlower,,,std::isalpha,,,std::isupper,,,std::iswprint,,,std::mbtowc,,,std::towupper,,,std::isblank,,,std::iswalnum,,,std::iswpunct,,,std::setlocale,,,std::wcscoll,,,std::iscntrl,,,std::iswalpha,,,std::iswspace,,,std::strcoll,,,std::wcstod,,,std::isdigit,,,std::iswblank,,,std::iswupper,,,std::strerror,,,std::wcstombs,,,std::isgraph,,,std::iswcntrl,,,std::iswxdigit,,,std::strtod,,,std::wcsxfrm,,,std::islower,,,std::iswctype,,,std::isxdigit...

POSIX还定义了一个名为“POSIX”的区域设置,它始终是可访问的,并且完全等同于默认的最小“C”区域设置。

POSIX还指定返回的指针,而不仅仅是指向字符串的内容,可能会因随后调用setlocale而失效。

二次

代码语言:javascript
复制
#include <cstdio>
#include <clocale>
#include <ctime>
#include <cwchar>
 
int main()
{
    // the C locale will be UTF-8 enabled English;
    // decimal dot will be German
    // date and time formatting will be Japanese
    std::setlocale(LC_ALL, "en_US.UTF-8");
    std::setlocale(LC_NUMERIC, "de_DE");
    std::setlocale(LC_TIME, "ja_JP");
 
    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);
}

二次

产出:

二次

代码语言:javascript
复制
Number: 3,14
Date: 月曜日 2011年12月19日 18時04分40秒

二次

另见

LC_ALLLC_COLLATELC_CTYPELC_MONETARYLC_NUMERICLC_TIME

locale categories for std::setlocale (macro constant)

locale

set of polymorphic facets that encapsulate cultural differences (class)

c.setlocale的文档

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com