蓝色天空网mbsky.com · 知识 · 实践 · 创造C · 本地阅读预览
教程 / 编程语言 / C

C 判断

讲解 C 语言真假判定规则(非零为真、零与 NULL 为假)、if/else/switch 及嵌套分支语句,以及三元条件运算符 ?: 的求值逻辑,并在 GCC C11 容器逐项实测。

本站独立整理的内容草稿。实例显示的是预期结果;浏览器、服务端运行与技术内容验收尚待完成,本页只用于阅读和图文核对。

学完这一章,你能够

真值判定规则与判断语句家族

判断结构让程序根据运行时条件决定执行哪条代码路径。C 语言采用简洁的标量真值约定:任意非零数值(包括负数)与非空指针均视为真(`true`),而数值 `0`、字符 `'\0'` 与空指针 `NULL` 均视为假(`false`)。

C 语言围绕条件控制提供了五种常见组织形式(详见下表):基础 `if` 语句、双分支 `if...else`(以及多分支 `else if` 阶梯)、多层嵌套 `if`、基于整型常量标签的多路 `switch` 语句以及嵌套 `switch` 语句。编写 `switch` 分支时,每个独立 `case` 末尾通常需放置 `break;` 以防意外贯穿(fall-through)至下一分支。

三元条件运算符 ? : 与奇偶判定实例

条件运算符 `Exp1 ? Exp2 : Exp3` 是 C 语言中唯一的三元运算符,适合在简洁表达式内替代简单 `if...else` 分支。求值时先计算 `Exp1`:若结果为真则仅计算 `Exp2` 并以其结果作为整个表达式的值;若为假则仅计算 `Exp3` 并返回其值(另一分支不会求值)。例如判断整数奇偶性时,可用 `(num % 2 == 0) ? "even" : "odd"` 一行完成分类。本站已在离线 GCC C11 容器实测非零/空指针真值、嵌套 `if`/`switch` 与正负整数及零的奇偶判定。

C 语言主要条件判断语句

语句描述
if 语句当条件表达式为真(非零)时执行其后的一条语句或代码块。
if...else 语句在条件为真时执行 if 分支,为假时执行可选的 else 分支。
嵌套 if 语句在 if 或 else 分支内部继续嵌套条件判断以表达复合层级规则。
switch 语句依据整型控制表达式的值直接分派到匹配的 case 常量标签或 default 分支。
嵌套 switch 语句在外层 switch 的某个 case 内再使用内层 switch 处理二级分类。

实测 C11 标量真值规则、嵌套 if/switch 分派与三元运算符奇偶判定

c · 阅读源码
#include <stdio.h>
#include <string.h>

static char classify_score(int score) {
    if (score >= 90) {
        return 'A';
    } else if (score >= 60) {
        if (score >= 75) {
            return 'B';
        }
        return 'C';
    } else {
        return 'D';
    }
}

static int nested_switch_code(int category, int sub) {
    switch (category) {
        case 1:
            switch (sub) {
                case 10: return 110;
                case 20: return 120;
                default: return 199;
            }
        case 2:
            return 200;
        default:
            return -1;
    }
}

int main(void) {
    int *p_null = NULL;
    int v_neg = -5;
    int t_neg = v_neg ? 1 : 0;
    int t_zero = 0 ? 1 : 0;
    int t_null = p_null ? 1 : 0;
    int c1_pass = (t_neg == 1 && t_zero == 0 && t_null == 0);

    char g1 = classify_score(95);
    char g2 = classify_score(78);
    char g3 = classify_score(55);
    int c2_pass = (g1 == 'A' && g2 == 'B' && g3 == 'D');

    int sw1 = nested_switch_code(1, 20);
    int sw2 = nested_switch_code(2, 0);
    int sw3 = nested_switch_code(9, 0);
    int c3_pass = (sw1 == 120 && sw2 == 200 && sw3 == -1);

    const char *p0 = (0 % 2 == 0) ? "even" : "odd";
    const char *p42 = (42 % 2 == 0) ? "even" : "odd";
    const char *p17 = (17 % 2 == 0) ? "even" : "odd";
    const char *pm3 = ((-3) % 2 == 0) ? "even" : "odd";
    int c4_pass = (strcmp(p0, "even") == 0 && strcmp(p42, "even") == 0 &&
                   strcmp(p17, "odd") == 0 && strcmp(pm3, "odd") == 0);

    printf("{\"runtime\": \"GCC C11 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [\n");
    printf("  {\"name\": \"truthiness_nonzero_and_null\", \"expected\": {\"neg\": 1, \"zero\": 0, \"null_ptr\": 0}, \"actual\": {\"neg\": %d, \"zero\": %d, \"null_ptr\": %d}, \"passed\": %s},\n",
           t_neg, t_zero, t_null, c1_pass ? "true" : "false");
    printf("  {\"name\": \"if_else_and_nested_if\", \"expected\": [\"A\", \"B\", \"D\"], \"actual\": [\"%c\", \"%c\", \"%c\"], \"passed\": %s},\n",
           g1, g2, g3, c2_pass ? "true" : "false");
    printf("  {\"name\": \"switch_and_nested_switch\", \"expected\": [120, 200, -1], \"actual\": [%d, %d, %d], \"passed\": %s},\n",
           sw1, sw2, sw3, c3_pass ? "true" : "false");
    printf("  {\"name\": \"ternary_even_odd\", \"expected\": [\"even\", \"even\", \"odd\", \"odd\"], \"actual\": [\"%s\", \"%s\", \"%s\", \"%s\"], \"passed\": %s}\n",
           p0, p42, p17, pm3, c4_pass ? "true" : "false");
    printf("]}\n");
    return 0;
}
预期结果 · 待实际运行核验
  • 确认 -5 为真而 0/NULL 为假,分数分级 A/B/D、嵌套 switch 返回 120/200/-1 及 0/42/17/-3 奇偶分类全部通过。
实测 C11 标量真值规则、嵌套 if/switch 分派与三元运算符奇偶判定

所需环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC 13.3.0 C11 linux/amd64,--network none)。

当前源码 SHA256:baffbf3e86584a24590a71814a6b888451c4f292f04d7a0c05c264b30e73d9a9

下载本站示例源码

本地运行

解压后按 README 说明在对应本地环境运行脚本。

下载C 判断实验

所需环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC 13.3.0 C11 linux/amd64,--network none)。

ZIP SHA256:6dff8c7a085909154ef158ac269a34755d40f8767bda633c53f0dac1137c54fa

常见错误与边界

  • 实际端口、文档资源与应用配置都应和测试条件一致。

练习与解答

在 C 语言中,判断负奇数(如 `-3`)是否为奇数时,为什么写 `n % 2 != 0` 比 `n % 2 == 1` 更安全?

展开参考解答

自 C99 起整数取余结果符号与被除数一致,因此 `(-3) % 2` 的结果是 `-1` 而非 `1`;使用 `n % 2 != 0`(或判断偶数的 `n % 2 == 0`)对正负整数都同样准确。

规范与官方依据