Menu
CoddyTech
课程

面向 Ruby 开发者的 C

从 Ruby 转向 C17:学习显式类型、指针、有界数组、字符串、所有权、结构体和模块化接口。构建一个由多个文件组成的库存账本,并移植两个 Ruby 程序。

Ruby 与 C 对照

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

  1. 输出与注释

    Ruby 的 puts 会用 to_s 转换它的参数,并为你添加换行符。C 的 printf 使用格式字符串,其中的转换说明符必须与参数类型匹配;%d 会输出 int。Ruby 的 format 也使用 %d,但它会进行转换或引发 ArgumentError;C 则会直接相信说明符。注释使用 // 或 /* ... */,而不是 #。在 C 的格式字符串中,换行符需要显式写出。

    Ruby
    1puts "value=7"
    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. 布尔值与分支

    Ruby 只将 nil 和 false 视为假值,因此 0 和空字符串都是真值,检查是否为空要用 empty?。在 C 中,标量零为假,非零标量为真,因此 0 的含义正好相反;非空指针为真,即使它指向空字符串也是如此。需要明确的布尔结果时,可以使用 stdbool.h 中的 bool。

    Ruby
    1n = 02s = ""3puts "#{n ? true : false} #{s ? true : false}"
    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=0; const char *s=""; printf("%s %s\n",n?"true":"false",s?"true":"false");11return 0;12}
  3. 数组需要长度

    Ruby 数组会携带长度,并且可以通过 push 或 << 增长。C 数组的元素数量固定,而函数的数组参数会调整为指针,不再携带元素数量。请单独传入 size_t 类型的长度,并遵守该长度;C 不会检查下标。

    Ruby
    1a = [2, 4, 6]2puts a.length
    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. 函数参数是值

    Ruby 会将每个参数作为对调用方对象的引用传递,而参数名是它的新局部名称。重新赋值给参数不会改变调用方的变量,但对数组参数调用 << 这样的修改操作会改变共享对象。C 参数接收的是值的副本。更改 int 参数不会改变调用方的变量。指针参数也会被复制,但它可以指向由调用方拥有、函数可能会修改的存储空间。

    Ruby
    1def change(n)2  n = 93end4x = 25change(x)6puts x
    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. 地址与解引用

    每个 Ruby 变量都引用一个对象,但 Ruby 不会把地址交给你供你追踪:object_id 是标识符,不能解引用。在 C 中,&object 会获取一个地址,而 *pointer 会访问指针指向的对象。只能解引用指向有效且仍然存活的正确类型对象的指针。空指针和悬空指针不是像 nil 这样的普通空值;使用它们会导致未定义行为,而不是 NoMethodError。

    Ruby
    1box = [2]2other = box3other[0] = 54puts box[0]
    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. 带状态结果的解析

    Ruby 中,Integer("12x") 会引发 ArgumentError,而 "12x".to_i 会悄悄返回 12,也就是数字前缀。C 库解析器会使用明确的状态信息。sscanf 会报告成功赋值的次数,但仅匹配前缀并不能证明整个输入有效,这又会导致 to_i 陷阱。单独进行一次验证可以强制使用严格的语法。

    Ruby
    1text = "12x"2puts text.match?(/\A[0-9]+\z/)
    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 道测验题

最后的 Ruby 移植

2 节课

更多面向 Ruby 开发者的课程

从其他语言出发学习 C

常见问题

面向 Ruby 开发者的 C 适合谁学习?

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

需要先会 C 吗?

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

面向 Ruby 开发者的 C 有多少节课?

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

面向 Ruby 开发者的 C 包含哪些内容?

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

面向 Ruby 开发者的 C 免费吗?

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

Coddy 编程语言插图

免费开始学习 面向 Ruby 开发者的 C

开始