你好, 世界

要解决的问题

多亏了 Brian Kernighan 教授(David 上 CS50 时他正是授课教师之一!),“hello, world” 已经被用数百种语言实现过。现在让我们把你的实现也加入其中!

请在名为 world 的文件夹中创建 hello.c,并用 C 实现一个程序,只打印 hello, world\n,仅此而已!

提示

下面就是你应该写的实际代码!(这个提示够直接吧?)不过,最好自己手动输入,而不是复制粘贴,这样你可以开始培养写代码的“肌肉记忆”。

#include <stdio.h>

int main(void)
{
    printf("hello, world\n");
}

演示

下面演示了编译并运行程序时应发生的情况。

如何开始

在终端窗口中单独执行 cd。你应该会看到终端提示符类似下面这样:

$

接下来执行

mkdir world

在 codespace 中创建名为 world 的文件夹。

然后执行

cd world

进入该文件夹。现在你应该会看到终端提示符显示为 world/ $。现在可以执行

code hello.c

to create 名为 hello.c ,并在其中编写你的代码。

如何测试

记住,你可以编译 hello.c with:

make hello

如果没有看到错误消息,就表示编译成功!可以用下面的命令确认:

ls

which should list not only hello.c (which is 源代码) but also hello (which is machine code).

如果看到错误消息,请尝试修复代码并再次编译。如果你不理解错误消息,可以尝试执行

help50 make hello

for advice.

代码成功编译后,可以用下面的命令运行程序:

./hello

正确性

执行下面的命令,使用 check50, a command-line program that will output happy faces whenever your code passes CS50’s automated tests and sad faces whenever it doesn’t!

check50 cs50/problems/2026/x/world

代码风格

执行下面的命令,使用 style50, a command-line program that will output additions (in green) and deletions (in red) that you should make to your program in order to improve its style. If you have trouble seeing those colors, style50 supports other modes too!

style50 hello.c

如何提交

No need to 提交 this one! Just a practice 练习!