Learn C the Hard Way: Dust Off That Compiler
After you have everything installed, you need to confirm that your compiler works. The easiest way to do that is to write a C program. Since you should already know at least one programming language, I believe you can start with a small but extensive example.
ex1.c
1#include 2 3/* This is a comment. */4intmain(intargc,char*argv[])5{6intdistance=100; 7 8// this is also a comment9 printf("You are %d miles away.\n",distance);10 11return0;12}
如果你有问题,runn的代码ing, watch the video for this exercise to see me do it first.
Breaking It Down
There are a few features of the C language in this code that you might or might not have figured out while you were typing it. I’ll break this down, line by line, quickly, and then we can do exercises to understand each part better. Don’t worry if you don’t understand everything in this breakdown. I am simply giving you a quick dive into C and承诺you will learn all of these concepts later in the book.
在这里’s a line-by-line description of the code:
- ex1.c:1Aninclude, and it is the way to import the contents of one file into this source file. C has a convention of using.h扩展headerfiles, which contain lists of functions to use in your program.
- ex1.c:3This is a multilinecomment, and you could put as many lines of text between the opening/*and closing*/characters as you want.
- ex1.c:4A more complex version of themainfunction you’ve been using so far. How C programs work is that the operating system loads your program, and then it runs the function namedmain. For the function to be totally complete it needs to return anintand take two parameters: anintfor the argument count and an array ofchar *strings for the arguments. Did that just fly over your head? Don’t worry, we’ll cover this soon.
- ex1.c:5To start the body of any function, you write a{character that indicates the beginning of ablock. In Python, you just did a:and indented. In other languages, you might have abeginordoword to start.
- ex1.c:6A variable declaration and assignment at the same time. This is how you create a variable, with the syntaxtype name = value;. In C, statements (except for logic) end in a;(semicolon) character.
- ex1.c:8Another kind of comment. It works like in Python or Ruby, where the comment starts at the//and goes until the end of the line.
- ex1.c:9A call to your old friendprintf. Like in many languages, function calls work with the syntaxname(arg1, arg2);and can have no arguments or any number of them. Theprintffunction is actually kind of weird in that it can take multiple arguments. You’ll see that later.
- ex1.c:11A return from the main function that gives the operating system (OS) your exit value. You may not be familiar with how UNIX software uses return codes, so we’ll cover that as well.
- ex1.c:12Finally, we end the main function with a closing brace}character, and that’s the end of the program.
There’s a lot of information in this breakdown, so study it line by line and make sure you at least have a grasp of what’s going on. You won’t know everything, but you can probably guess before we continue.