Menu
CoddyTech
课程

面向 Dart 开发者的 C 语言课程

从 Dart 过渡到 C17:学习显式接口、指针、数组、字符串、所有权和结构体。构建一个由多个文件组成的库存台账,并移植两个完整程序。

Dart 与 C 对照

同一个程序分别用 Dart 和 C 编写,取自本课程的课时。

  1. 打印与注释

    Dart 的 print 接受任何对象,字符串插值会调用 toString,因此值不会被格式化成错误的类型。C 的 printf 会遵循格式字符串:每个转换说明符都必须与提升后的参数类型匹配,不匹配会导致未定义行为,而不是抛出异常。这两种语言都使用 // 和 /* */ 注释;Dart 的 /// 文档注释在 C 中只是普通的单行注释。

    Dart
    1void main() {2  // 插值时会对该值调用 toString()。3  int value = 7;4  print('value=$value');5}
    C
    1#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}
  2. 布尔值与分支

    Dart 要求每个条件的静态类型都是 bool:对于 String,if (s) 无法编译,因此你要用 isEmpty 检查是否为空。C 接受任何标量作为条件,指向空字符串的指针不是空指针,因此条件为真。

    Dart
    1void main() {2  String s = '';3  print(s.isEmpty);4}
    C
    1#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}
  3. 数组需要长度

    Dart 的 List 是一个始终知道自身长度的对象,把它传给函数时,传递的是对同一对象的引用。C 数组参数会变成指针,从而丢失边界信息;你应该显式传递 size_t 长度,而不是查找 length 属性。

    Dart
    1void main() {2  List<int> a = [2, 4, 6];3  print(a.length);4}
    C
    1#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}
  4. 函数参数传递的是值

    Dart 会将对象引用的副本传给参数。int、double 和 String 对象都是不可变的,给参数赋值只会重新绑定这个局部名称,因此调用方的变量不会改变。C 也会传递值:int 参数是副本,传递指针时会复制指针,同时允许访问它所指向的对象。

    Dart
    1void change(int x) {2  x = 9;3}4
    5void main() {6  var x = 2;7  change(x);8  print(x);9}
    C
    1#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}
  5. 地址与解引用

    Dart 不会向你显示地址。变量保存的是对象引用,两个变量可以指向同一个对象;只要还有任何东西能访问该对象,垃圾回收器就会让它保持存活;除了 dart:ffi,Dart 没有取地址运算符。C 中的 &n 会获取对象的地址,*p 则会间接读取或写入该对象;对象的生命周期有效性由程序员保证,而不是由垃圾回收机制保证。

    Dart
    1class Cell {2  int value;3  Cell(this.value);4}5
    6void main() {7  final cell = Cell(2);8  final alias = cell;9  alias.value = 5;10  print(cell.value);11}
    C
    1#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}
  6. 解析并返回状态结果

    Dart 提供两种检查整个输入的方法。int.tryParse 会先去掉文本两端的空白字符,只有文本是完整整数时才会返回整数,否则返回 null。只有使用 ^ 和 $ 锚定后,RegExp 才会检查整个字符串,因为 hasMatch 会在任意位置搜索。C 的 sscanf 可能只成功读取数字前缀;完整输入验证必须作为解析器约定中单独且明确的一部分。

    Dart
    1void main() {2  const text = '12x';3  final digits = RegExp(r'^[0-9]+$');4  print('${digits.hasMatch(text)} ${int.tryParse(text)}');5}
    C
    1#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 ok=*s!=0;for(size_t i=0;s[i];i++)if(s[i]<'0'||s[i]>'9')ok=0;printf("%d\n",ok);11return 0;12}

课程大纲

10 章48 节课1 个项目48 个挑战99 道测验题

最终源码移植

2 节课

更多面向 Dart 开发者的课程

从其他语言出发学习 C

常见问题

面向 Dart 开发者的 C 语言课程 适合谁学习?

适合已经会写 Dart、想学 C 的人。每节课都从你熟悉的 Dart 代码出发,展示 C 如何实现同样的事情,所以你可以跳过入门基础。

需要先会 C 吗?

不需要。课程从你的第一个 C 程序开始。你需要掌握 Dart 基础:变量、条件、循环、函数和集合。

面向 Dart 开发者的 C 语言课程 有多少节课?

面向 Dart 开发者的 C 语言课程 包含 48 节互动课,你可以按自己的节奏完成。

面向 Dart 开发者的 C 语言课程 包含哪些内容?

本课程分为以下几章:编译与计算, 条件与循环, 数组与文本, 函数与接口, and more。

面向 Dart 开发者的 C 语言课程 免费吗?

是的。Coddy 上的每门课程都可以免费开始。你可以直接在浏览器中完成课程,无需下载,也无需注册。

Coddy 编程语言插图

免费开始学习 面向 Dart 开发者的 C 语言课程

开始