#include <math.h>
#include <stdio.h>
#include <string.h>
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'
int main(void) {
int dec_v = 85;
int oct_v = 0213;
int hex_v = 0x4b;
long hex_l = 0xFeeL;
unsigned long ul_v = 30ul;
float f_v = 3.14f;
long double ld_v = 314159E-5L;
int c1_pass = (dec_v == 85 && oct_v == 139 && hex_v == 75 && hex_l == 4078L &&
ul_v == 30UL && fabs((double)f_v - 3.14) < 0.001 && fabsl(ld_v - 3.14159L) < 1e-6L);
char myChar = 'a';
int ascii_a = (int)myChar;
int esc_ok = ('\t' == 9 && '\n' == 10 && '\\' == 92 && '\'' == 39 &&
'\"' == 34 && '\?' == 63 && '\101' == 'A' && '\x41' == 'A');
int c2_pass = (ascii_a == 97 && esc_ok);
const char *s1 = "hello, dear";
const char *s2 = "hello, \
dear";
const char *s3 = "hello, " "d" "ear";
char arr[] = "Hello, world!";
int c3_pass = (strcmp(s1, s2) == 0 && strcmp(s1, s3) == 0 &&
sizeof(arr) == 14 && arr[13] == '\0');
int macro_area = LENGTH * WIDTH;
const int C_LEN = 10;
const int C_WID = 5;
int const_area = C_LEN * C_WID;
int inner_area = 0;
{
const int C_LEN = 20;
inner_area = C_LEN * C_WID;
}
int c4_pass = (macro_area == 50 && const_area == 50 && inner_area == 100 && NEWLINE == '\n');
printf("{\"runtime\": \"GCC C11 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [\n");
printf(" {\"name\": \"integer_and_float_literals\", \"expected\": {\"oct\": 139, \"hex\": 75, \"hex_long\": 4078, \"ul\": 30}, \"actual\": {\"oct\": %d, \"hex\": %d, \"hex_long\": %ld, \"ul\": %lu}, \"passed\": %s},\n",
oct_v, hex_v, hex_l, ul_v, c1_pass ? "true" : "false");
printf(" {\"name\": \"char_escapes_and_ascii\", \"expected\": {\"ascii_a\": 97, \"escapes_ok\": true}, \"actual\": {\"ascii_a\": %d, \"escapes_ok\": %s}, \"passed\": %s},\n",
ascii_a, esc_ok ? "true" : "false", c2_pass ? "true" : "false");
printf(" {\"name\": \"string_literal_concat_and_nul\", \"expected\": {\"concat_equal\": true, \"sizeof_arr\": 14}, \"actual\": {\"concat_equal\": %s, \"sizeof_arr\": %zu}, \"passed\": %s},\n",
(strcmp(s1, s2) == 0 && strcmp(s1, s3) == 0) ? "true" : "false", sizeof(arr), c3_pass ? "true" : "false");
printf(" {\"name\": \"define_vs_const_constants\", \"expected\": {\"macro_area\": 50, \"const_area\": 50, \"inner_shadow_area\": 100}, \"actual\": {\"macro_area\": %d, \"const_area\": %d, \"inner_shadow_area\": %d}, \"passed\": %s}\n",
macro_area, const_area, inner_area, 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-constants-lab/explore-constants.c
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-constants.c,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC 13.3.0 C11 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联63章) · 含当前章节或实例 · mbskycom202612a3210.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3214.zip