C语言标准库函数setlocale用法详解

C语言标准库函数setlocale用法详解

C语言标准库函数setlocale定义在 <locale.h> 头文件中(在C++编程中,这个头文件等价于 <clocale> )。

原型:

c 复制代码
char* setlocale (int category, const char* locale);

函数参数解析

category

int类型,可以取以下几个值:

值 影响范围
LC_ALL 全部更改为传入的locale
LC_COLLATE 影响字符串比较函数 strcol1()和 strxfrm()。
LC_CTYPE 影响字符处理函数的行为。
LC_MONETARY 影响货币格式
LC_NUMERIC 影响 printf() 的数字格式
LC_TIME 影响时间格式 strftime()和 wcsftime()。

locale

locale参数是字符串(char*)类型,有以下几种写法:

| "language_country-region\[.code-page]"

  1. 名称写法,例如中文多字节代码页""
  2. Windows特有的代码页写法 ".code-page",例如936代码页的locale,".936";65001代码页的locale,".65001"。
  3. 通用选项"C"、""、NULL

常见的locale本地化参数效果如下表所示:

值 效果
"C" 采用仅支持ASCII的最小本地化配置
NULL 不改变任何设置,函数仅返回当前的locale名称
"" 采用当前系统环境的默认代码页。例如中文Windows系统默认的locale为ANSI/OEM 936
".utf8" 使用UTF-8本地化
".utf-8" 使用UTF-8本地化
"en_us.utf8" 使用UTF-8本地化
"LC_MONETARY=en-US;LC_TIME=ja-JP" 货币使用en-US本地化(美国货币符号),时间使用ja-JP本地化(日本时间格式)

更多设置格式,可参考文末UCRT Locale names, Languages, and Country/Region strings链接。

应用

本节给出setlocale函数的具体应用,以便读者体会其功能。

应用1:在控制台打印宽字符

为输出宽字符,调用setlocale是必要的。

看一段例程:

c 复制代码
/* setlocale example */
#include <stdio.h>      /* printf */
#include <time.h>       /* time_t, struct tm, time, localtime, strftime */
#include <locale.h>     /* struct lconv, setlocale, localeconv */

int main ()
{
    wchar_t* pstr0=L"Wide Character Test 0\n";
    wprintf(pstr0);
    wchar_t* pstr1=L"宽字符测试1\n";
    wprintf(pstr1);
    setlocale(LC_ALL,"");
    wchar_t* pstr2=L"宽字符测试2\n";
    wprintf(pstr2);
    return 0;
}

运行后得到如下输出:

Wide Character Test 0

宽字符测试2

也就是说,调用setlocale之前,程序的默认本地化环境是 "C" ,只支持 ASCII 字符,中日韩等非ASCII字符会被视为无效字符。

而wprintf函数输出字符串到控制台时,需要先将UTF-16编码的字符数据转换成多字节编码(如GBK、UTF-8等)数据。调用setlocale之前,包含了中日韩字符的字符串转换过程必失败,从而导致L"宽字符测试1"未被输出。

将setlocale的调用位置放到程序最开始,再次尝试:

c 复制代码
/* setlocale example */
#include <stdio.h>      /* printf */
#include <time.h>       /* time_t, struct tm, time, localtime, strftime */
#include <locale.h>     /* struct lconv, setlocale, localeconv */

int main ()
{
    setlocale(LC_ALL,"");
    wchar_t* pstr0=L"Wide Character Test 0\n";
    wprintf(pstr0);
    wchar_t* pstr1=L"宽字符测试1\n";
    wprintf(pstr1);
    wchar_t* pstr2=L"宽字符测试2\n";
    wprintf(pstr2);
    return 0;
}

运行后得到如下输出:

Wide Character Test 0

宽字符测试1

宽字符测试2

应用2:控制货币、时间日期格式

请运行代码以切实体会setlocale的不同参数对货币符号、时间日期格式的影响:

c 复制代码
/* setlocale example */
#include <stdio.h>      /* printf */
#include <time.h>       /* time_t, struct tm, time, localtime, strftime */
#include <locale.h>     /* struct lconv, setlocale, localeconv */

int main ()
{
  time_t rawtime;
  struct tm * timeinfo;
  char buffer [80];

  struct lconv * lc;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );

  int twice=0;

  do {
    printf ("Locale is: %s\n", setlocale(LC_ALL,NULL) );

    strftime (buffer,80,"%c",timeinfo);
    printf ("Date is: %s\n",buffer);

    lc = localeconv ();
    printf ("Currency symbol is: %s\n-\n",lc->currency_symbol);

    setlocale (LC_ALL,"");
  } while (!twice++);

  return 0;

命令行输出结果为:

参考

来自cplusplus.com的setlocale函数定义与示例

微软文档:

setlocale, _wsetlocale
UCRT Locale names, Languages, and Country/Region strings

相关推荐
·present·11 小时前
CSAPP(lecture 4_01)
c语言·c++
本地化文档15 小时前
llvm-libc-docs-l10n
c语言·github·gitcode·sphinx·llvm·crowdin
Felven16 小时前
B. Collecting Game
c语言
Felven16 小时前
C. Removing Smallest Multiples
c语言·开发语言
水饺编程18 小时前
第2章:Linux 基础,重启命令
linux·c语言·汇编
inrichtt19 小时前
第十一课时:串口中断接收与回调函数实战解析
c语言·数据结构·stm32·单片机·嵌入式硬件
潜创微科技20 小时前
CH592:集成 BLE 的青稞 RISC-V 无线 MCU
c语言·单片机·risc-v·无线投屏·低延迟·沁恒
学逆向的20 小时前
C 可变量参数
c语言·开发语言·网络安全
小莞尔21 小时前
【51单片机】【protues仿真】基于51单片机六路抢答器系统
c语言·单片机·嵌入式硬件·物联网·51单片机
心中有你02141 天前
C 语言文件操作详解:从写入、追加到读取与定位的完整 实战
c语言·开发语言