#include <stddef.h>
#include <stdio.h>
#include <string.h>
int main(void) {
char s_chars[8] = {'B', 'L', 'U', 'E', 'S', 'K', 'Y', '\0'};
char s_lit[] = "BLUESKY";
size_t sz1 = sizeof(s_chars);
size_t sz2 = sizeof(s_lit);
size_t len = strlen(s_lit);
int eq = (strcmp(s_chars, s_lit) == 0 && s_lit[7] == '\0');
int c1_pass = (sz1 == 8 && sz2 == 8 && len == 7 && eq);
char str1[32] = "bluesky";
char str2[16] = "clang";
char str3[32];
strcpy(str3, str1);
strcat(str1, str2);
size_t joined_len = strlen(str1);
int c2_pass = (strcmp(str3, "bluesky") == 0 && strcmp(str1, "blueskyclang") == 0 && joined_len == 12);
int cmp_lt = (strcmp("apple", "banana") < 0);
int cmp_eq = (strcmp("same", "same") == 0);
const char *hay1 = "bluesky-c";
const char *p_chr = strchr(hay1, '-');
ptrdiff_t idx_chr = p_chr ? (p_chr - hay1) : -1;
const char *hay2 = "c11 string library";
const char *p_sub = strstr(hay2, "string");
ptrdiff_t idx_sub = p_sub ? (p_sub - hay2) : -1;
int c3_pass = (cmp_lt && cmp_eq && idx_chr == 7 && idx_sub == 4);
char small_buf[8];
const char *volatile a_part = "hello";
const char *volatile b_part = "world";
int needed = snprintf(small_buf, sizeof(small_buf), "%s-%s", a_part, b_part);
int c4_pass = (needed == 11 && strcmp(small_buf, "hello-w") == 0 && small_buf[7] == '\0');
printf("{\"runtime\": \"GCC C11 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [\n");
printf(" {\"name\": \"char_array_vs_string_literal\", \"expected\": {\"sizeof\": 8, \"strlen\": 7, \"equal\": true}, \"actual\": {\"sizeof\": %zu, \"strlen\": %zu, \"equal\": %s}, \"passed\": %s},\n",
sz2, len, eq ? "true" : "false", c1_pass ? "true" : "false");
printf(" {\"name\": \"strcpy_strcat_strlen\", \"expected\": {\"copied\": \"bluesky\", \"joined\": \"blueskyclang\", \"len\": 12}, \"actual\": {\"copied\": \"%s\", \"joined\": \"%s\", \"len\": %zu}, \"passed\": %s},\n",
str3, str1, joined_len, c2_pass ? "true" : "false");
printf(" {\"name\": \"strcmp_strchr_strstr\", \"expected\": {\"lt\": true, \"eq\": true, \"chr_idx\": 7, \"sub_idx\": 4}, \"actual\": {\"lt\": %s, \"eq\": %s, \"chr_idx\": %td, \"sub_idx\": %td}, \"passed\": %s},\n",
cmp_lt ? "true" : "false", cmp_eq ? "true" : "false", idx_chr, idx_sub, c3_pass ? "true" : "false");
printf(" {\"name\": \"safe_snprintf_truncation\", \"expected\": {\"needed\": 11, \"truncated\": \"hello-w\"}, \"actual\": {\"needed\": %d, \"truncated\": \"%s\"}, \"passed\": %s}\n",
needed, small_buf, c4_pass ? "true" : "false");
printf("]}\n");
return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC 13.3.0 C11 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC 13.3.0 C11 linux/amd64,--network none)。
入口文件:projects/c-strings-lab/explore-strings.c
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-strings.c,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC 13.3.0 C11 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联63章) · 含当前章节或实例 · mbskycom202612a3210.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3262.zip