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

相关推荐
lcj25112 小时前
深入理解指针(4):qsort 函数 & 通过冒泡排序实现
c语言·数据结构·算法
4311媒体网3 小时前
C语言操作符全解析 C语言操作符详解
java·c语言·jvm
二年级程序员3 小时前
一篇文章掌握“顺序表”
c语言·数据结构
傻乐u兔4 小时前
C语言进阶————指针4
c语言·开发语言
历程里程碑4 小时前
Linux22 文件系统
linux·运维·c语言·开发语言·数据结构·c++·算法
2601_9491465312 小时前
C语言语音通知接口接入教程:如何使用C语言直接调用语音预警API
c语言·开发语言
知南x14 小时前
【Ascend C系列课程(高级)】(1) 算子调试+调优
c语言·开发语言
2的n次方_15 小时前
Runtime 执行提交机制:NPU 硬件队列的管理与任务原子化下发
c语言·开发语言
凡人叶枫16 小时前
C++中智能指针详解(Linux实战版)| 彻底解决内存泄漏,新手也能吃透
java·linux·c语言·开发语言·c++·嵌入式开发
凡人叶枫18 小时前
C++中输入、输出和文件操作详解(Linux实战版)| 从基础到项目落地,避坑指南
linux·服务器·c语言·开发语言·c++