冲刺 · 变式 3:逗号表达式的值
逗号表达式 (e1, e2, e3) 从左往右依次计算,整个的值是最后一个。逗号的优先级最低,比赋值还低。 #include <stdio.h> int main(void) { int a = 3, b = 5, c
冲刺 · 变式 4:?: 连着写
e1 ? e2 : e3:e1 为真取 e2,否则取 e3。它是右结合的:a ? b : c ? d : e 等价于 a ? b : (c ? d : e)。 #include <stdio.h> int main(void)
冲刺 · else 和哪个 if 配对
#include <stdio.h> int main(void) { int a = 1, b = 2, c = 3; if (a > b) if (b > c) c = 10;
冲刺 · switch 没写 break
#include <stdio.h> int main(void) { int k = 2, s = 0; switch (k) { case 1: s += 1; case 2: s
冲刺 · while (i++ < 5) 转几圈
#include <stdio.h> int main(void) { int i = 0, n = 0; while (i++ < 5) n++; printf("%d
冲刺 · 读程序题:strlen 和 sizeof
两个最容易混的「长度」: strlen(s):库函数(在 <string.h> 里),从头数到 '\0' 为止的字符个数,不算 '\0'; sizeof(s):运算符,数组一共占多少字节。定义时写
冲刺 · 读程序题:传值还是传地址
C 语言的函数参数都是传值:调用时把实参的值复制一份给形参,函数里改形参,改的是副本。 想在函数里改主调函数的变量,就把它的地址传进去,函数里用 *p 去改那个地址上的值。 #include <stdio.h> void f(
冲刺 · 读程序题:static 局部变量
函数里的普通局部变量(auto)每次调用都重新创建、重新初始化,函数结束就消失。 加了 static 的局部变量只在第一次初始化,函数结束后值保留,下次调用接着用;但它仍然只能在这个函数里访问。 #include <stdio.h&g
冲刺 · 母题:数组初始化和下标范围
两条规则:数组初始化时只给了前几个值,后面的自动补 0;int a[5] 的下标是 0~4,C 编译器不检查越界。 另外 p 指向 a[0] 时,*(p + i) 就是 a[i]。 #include <stdio.h> int
冲刺 · 变式 1:*p++ 和 (*p)++
*p++:++ 作用在 p 上(后置),先取 *p 的值,再让 p 指向下一个元素。(*p)++:括号让 ++ 作用在 *p 上,指针不动、元素加 1。 #include <stdio.h> int main(void) {