#include <stdio.h>
#include <locale.h>
#include <string.h>
#include <limits.h>
#include <stdbool.h>
int main(void) {
const char *applied = setlocale(LC_ALL, "C");
const char *q_all = setlocale(LC_ALL, NULL);
const char *q_col = setlocale(LC_COLLATE, NULL);
const char *q_cty = setlocale(LC_CTYPE, NULL);
const char *q_mon = setlocale(LC_MONETARY, NULL);
const char *q_num = setlocale(LC_NUMERIC, NULL);
const char *q_tim = setlocale(LC_TIME, NULL);
int all_c = (
applied && strcmp(applied, "C") == 0 &&
q_all && strcmp(q_all, "C") == 0 &&
q_col && strcmp(q_col, "C") == 0 &&
q_cty && strcmp(q_cty, "C") == 0 &&
q_mon && strcmp(q_mon, "C") == 0 &&
q_num && strcmp(q_num, "C") == 0 &&
q_tim && strcmp(q_tim, "C") == 0
) ? 1 : 0;
bool c1_pass = (all_c == 1);
struct lconv *lc = localeconv();
int dec_ok = (lc && strcmp(lc->decimal_point, ".") == 0) ? 1 : 0;
int thou_ok = (lc && strcmp(lc->thousands_sep, "") == 0) ? 1 : 0;
int curr_ok = (lc && strcmp(lc->currency_symbol, "") == 0 && strcmp(lc->int_curr_symbol, "") == 0) ? 1 : 0;
int posn_ok = (lc && lc->frac_digits == CHAR_MAX && lc->p_sign_posn == CHAR_MAX && lc->n_sign_posn == CHAR_MAX) ? 1 : 0;
bool c2_pass = (dec_ok == 1 && thou_ok == 1 && curr_ok == 1 && posn_ok == 1);
const char *bad = setlocale(LC_ALL, "invalid_mbsky_locale_999");
const char *after = setlocale(LC_NUMERIC, NULL);
int preserved = (bad == NULL && after != NULL && strcmp(after, "C") == 0) ? 1 : 0;
bool c3_pass = (preserved == 1);
printf(
"{"
"\"runtime\":\"GCC C11 (mstorsjo/llvm-mingw:20260616)\","
"\"cases\":["
"{\"name\":\"setlocale_c_categories\",\"expected\":1,\"actual\":%d,\"passed\":%s},"
"{\"name\":\"localeconv_c_fields\",\"expected\":\"dec=1,thou=1,curr=1,posn=1\",\"actual\":\"dec=%d,thou=%d,curr=%d,posn=%d\",\"passed\":%s},"
"{\"name\":\"invalid_locale_preserves_state\",\"expected\":1,\"actual\":%d,\"passed\":%s}"
"]"
"}\n",
all_c, c1_pass ? "true" : "false",
dec_ok, thou_ok, curr_ok, posn_ok, c2_pass ? "true" : "false",
preserved, c3_pass ? "true" : "false"
);
return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。
入口文件:projects/c-standard-library-locale-h-lab/explore-standard-library-locale-h.c
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-standard-library-locale-h.c,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3248.zip
课程关联工程包(登记关联63章) · 含当前章节或实例 · mbskycom202612a3210.zip