Swift 与 C 对照
同一个程序分别用 Swift 和 C 编写,取自本课程的课时。
打印与注释
Swift 字符串插值会在编译时进行检查:\(value) 接受任意值,并通过该值的描述进行格式化,因此不会发生类型不匹配。C 的 printf 则依赖可变参数格式字符串,如果转换说明符与参数不匹配,可能会导致未定义行为。
Swift1print("value=\(7)")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}布尔值与分支
Swift 条件必须是 Bool:Int、可选值或 String 都不会被测试其真值,因此你要写 != nil、if let 或 isEmpty。C 接受任意标量作为条件,因此指向空字符串的非空指针为真;指针是否存在和它指向的字符串是否为空是两个不同的问题。
Swift1let s = ""2print(s.isEmpty)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}数组需要长度
Swift 数组是一个会携带自身元素个数的值,ArraySlice 则会携带自身的边界,无论你在哪里传递它们。C 数组作为参数传递给函数时会变成指针,而这个指针既不携带长度,也不保证经过检查的生命周期;你需要单独传递大小,并自行确保两者保持一致。
Swift1let a = [2, 4, 6]2print(a.count)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}函数参数是值
Swift 参数是常量:给参数赋值会导致编译错误,因此需要临时副本的函数会用 var 为参数创建同名变量,而要修改调用方的变量,则需要使用 inout 参数,并在调用处使用 &x。C 始终会复制参数值,函数可以随意给自己的副本赋值,而要更新调用方的变量,则只能通过显式的指针参数来实现。
Swift1func change(_ n: Int) {2 var n = n3 n = 94 _ = n5}6var x = 27change(x)8print(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}地址与解引用
Swift 的 &n 用于 inout 参数时,看起来像 C 的取地址运算,但它只在调用期间授予独占访问权限,而且编译器绝不会让这种访问逃逸。原始指针以 UnsafeMutablePointer 的形式存在,名称中标明了不安全。C 的 int * 可以更新相同的数据,但不提供独占性或生命周期证明。非空并不意味着指针仍然有效或合法。
Swift1func setFive(_ p: inout Int) {2 p = 53}4var n = 25setFive(&n)6print(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}使用状态结果进行解析
Swift 的 Int("12x") 是一种可失败初始化器:除非整个字符串都是有效整数,否则它会返回 nil,因此 "12x"、" 12" 和空字符串都会解析失败。像 strtol、atoi 和 sscanf 这样的 C 前缀解析器,可能会在遇到后面的字节之前,成功解析开头的 12;必须显式检查整个输入是否有效,不能仅凭某个赋值判断。
Swift1print(Int("12x") != nil)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 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 道测验题常见问题
面向 Swift 开发者的 C 适合谁学习?
适合已经会写 Swift、想学 C 的人。每节课都从你熟悉的 Swift 代码出发,展示 C 如何实现同样的事情,所以你可以跳过入门基础。
需要先会 C 吗?
不需要。课程从你的第一个 C 程序开始。你需要掌握 Swift 基础:变量、条件、循环、函数和集合。
面向 Swift 开发者的 C 有多少节课?
面向 Swift 开发者的 C 包含 48 节互动课,你可以按自己的节奏完成。
面向 Swift 开发者的 C 包含哪些内容?
本课程分为以下几章:编译与计算, 决策与循环, 数组与文本, 函数与接口, and more。
面向 Swift 开发者的 C 免费吗?
是的。Coddy 上的每门课程都可以免费开始。你可以直接在浏览器中完成课程,无需下载,也无需注册。