返回介绍

Exercise 1: Dust Off That Compiler

发布于 2025-03-08 19:42:05 字数 1886 浏览 0 评论 0 收藏 0

Here is a simple first program you can make in C:

int main(int argc, char *argv[])
{
    puts("Hello world.");

    return 0;
}

You can put this into a ex1.c then type:

$ make ex1
cc     ex1.c   -o ex1

Your computer may use a slightly different command, but the end result should be a file named ex1 that you can run.

What You Should See

You can now run the program and see the output.

$ ./ex1
Hello world.

If you don't then go back and fix it.

How To Break It

In this book I'm going to have a small section for each program on how to break the program. I'll have you do odd things to the programs, run them in weird ways, or change code so that you can see crashes and compiler errors.

For this program, rebuild it with all compiler warnings on:

$ rm ex1
$ CFLAGS="-Wall" make ex1
cc -Wall    ex1.c   -o ex1
ex1.c: In function 'main':
ex1.c:3: warning: implicit declaration of function 'puts'
$ ./ex1
Hello world.
$

Now you are getting a warning that says the function "puts" is implicitly declared. The C compiler is smart enough to figure out what you want, but you should be getting rid of all compiler warnings when you can. How you do this is add the following line to the top of ex1.c and recompile:

#include <stdio.h>

Now do the make again like you just did and you'll see the warning go away.

Extra Credit

  • Open the ex1 file in your text editor and change or delete random parts. Try running it and see what happens.
  • Print out 5 more lines of text or something more complex than hello world.
  • Run man 3 puts and read about this function and many others.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文