TypeScript 与 C 对照
同一个程序分别用 TypeScript 和 C 编写,取自本课程的课时。
输出与注释
TypeScript 的 console.log 会在运行时将任意值转换为文本,并自动换行;模板字面量则使用 ${...} 插入值。C 的 printf 接受可变数量的参数,并通过格式字符串中的转换说明符来描述这些参数。它不会帮你转换参数:参数类型不匹配可能导致未定义行为,而不是类型错误或抛出异常。
TypeScript1// 模板字符串用于生成这段文本。2const value: number = 7;3console.log(`value=${value}`);C1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8 9int main(void) {10printf("value=%d\n",7);11return 0;12}布尔值与分支
TypeScript 条件使用 JavaScript 的真值规则:0、NaN、""、null 和 undefined 都是假值,因此空字符串测试结果为 false。C 支持对标量进行真值测试,但 C 字符串通过指针访问,指向空字符串的非空指针为真;要测试字符串是否为空,需要检查终止符,例如 s[0] == '\0',或者检查长度。
TypeScript1const s: string = "";2console.log(s ? "true" : "false");C1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8 9int main(void) {10const char *s=""; printf("%s\n",s?"true":"false");11return 0;12}数组需要长度
TypeScript 数组是按引用传递的对象,任何接收数组的函数都可以读取它的 length。C 的指针参数不包含数组长度范围,因此要单独传入长度;只有在实际数组类型可见时,才能使用 sizeof 计算元素数量。
TypeScript1const a: number[] = [2, 4, 6];2console.log(a.length);C1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8 9int main(void) {10int a[]={2,4,6};printf("%zu\n",sizeof a/sizeof a[0]);11return 0;12}函数参数是值
TypeScript 会按值传递每个参数。对于 number,传递的值就是数字本身;对于对象,传递的值是一个引用,因此函数可以更改对象的属性,但无法让调用者的变量指向其他位置。TypeScript 没有 ref 或 out 参数:要更新调用者的数字,你需要返回新值。C 的普通参数也是如此,并通过显式的指针参数来更新调用者的变量。
TypeScript1function change(x: number): void {2 x = 9;3}4 5let x = 2;6change(x);7console.log(x);C1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8static void change(int n){n=9;(void)n;}9int main(void) {10int x=2;change(x);printf("%d\n",x);11return 0;12}地址与解引用
TypeScript 没有地址:你无法获取局部变量的引用。对象始终通过运行时管理的引用来访问,只要仍有任何途径可以访问对象,垃圾回收器就会让对象保持存活。因此,要让函数更新一个值,你需要传入一个包含该值的对象。C 直接使用地址和解引用:&n 获取 n 的地址,*p 则读取或写入 p 指向的内容。C 不会跟踪生命周期。
TypeScript1function set(box: { n: number }): void {2 box.n = 5;3}4 5const box = { n: 2 };6set(box);7console.log(box.n);C1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8 9int main(void) {10int n=2;int *p=&n;*p=5;printf("%d\n",n);11return 0;12}通过状态结果进行解析
TypeScript 的数字解析会将失败反映在值中:parseInt 会读取最长的数字前缀,并忽略其余部分;而 Number 会返回 NaN,从而拒绝整段文本。NaN 的类型仍然是 number,因此编译器无法发现你漏掉了检查。C 的 sscanf 会返回已赋值的字段数,并且和 parseInt 一样可以接受前缀:仅仅成功赋值,并不能证明整个输入有效。
TypeScript1const text = "12x";2console.log(parseInt(text, 10), Number(text));C1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <stddef.h>5#include <stdbool.h>6#include <limits.h>7#include <errno.h>8 9int main(void) {10const char *s="12x";int n=0,used=0;11int fields=sscanf(s,"%d%n",&n,&used);12printf("%d %d %d\n",fields,n,s[used]==0);13return 0;14}
课程大纲
10 章48 节课1 个项目48 个挑战99 道测验题常见问题
面向 TypeScript 开发者的 C 适合谁学习?
适合已经会写 TypeScript、想学 C 的人。每节课都从你熟悉的 TypeScript 代码出发,展示 C 如何实现同样的事情,所以你可以跳过入门基础。
需要先会 C 吗?
不需要。课程从你的第一个 C 程序开始。你需要掌握 TypeScript 基础:变量、条件、循环、函数和集合。
面向 TypeScript 开发者的 C 有多少节课?
面向 TypeScript 开发者的 C 包含 48 节互动课,你可以按自己的节奏完成。
面向 TypeScript 开发者的 C 包含哪些内容?
本课程分为以下几章:编译与计算, 条件与循环, 数组与文本, 函数与接口, and more。
面向 TypeScript 开发者的 C 免费吗?
是的。Coddy 上的每门课程都可以免费开始。你可以直接在浏览器中完成课程,无需下载,也无需注册。