Appearance
02 条件分支 · 温度分档
顺着 if 的判断顺序找出边界属于哪一档。
源码
main.c
c
#include <stdio.h>
static const char *level(int celsius)
{
if (celsius >= 40) {
return "hot";
}
if (celsius >= 30) {
return "warm";
}
return "normal";
}
int main(void)
{
printf("%s %s %s\n", level(29), level(30), level(40));
return 0;
}主机运行
在仓库根目录执行。Windows 可以将输出名写成 reading-02.exe,再从终端运行。
sh
gcc -std=c11 -Wall -Wextra -pedantic examples/c-reading/02/main.c -o reading-02
./reading-02先沿 main() 预测打印结果,再展开核对。
主机输出与手推
text
normal warm hot29 走到最后一个分支,30 命中 warm,40 优先命中 hot。
练习
加入负温档,并检查 -1 与 0。
验证记录
主机版使用 GCC 编译运行,输出与跟读材料逐项核对。