See HowToUseTheComputingFacilities for details of particular commands. The basic steps are
Creating the program with a text editor of your choosing. (I like vim for long programs and cat for very short ones.)
Compiling it with gcc.
- Running it.
If any of these steps fail, the next step is debugging. We'll talk about debugging elsewhere.
1. Creating the program
Use your favorite text editor. The program file should have a name of the form foo.c; the .c at the end tells the C compiler the contents are C source code. Here is a typical C program:
1 #include <stdio.h>
2
3 int
4 main(int argc, char **argv)
5 {
6 int i;
7
8 puts("Now I will count from 1 to 10");
9 for(i = 1; i <= 10; i++) {
10 printf("%d\n", i);
11 }
12
13 return 0;
14 }
2. Compiling and running a program
Here's what happens when I compile and run it on the Zoo:
$ gcc -o count count.c $ ./count Now I will count from 1 to 10 1 2 3 4 5 6 7 8 9 10 $
The first line is the command to compile the program. The second line runs the output file count. Calling it ./count is necessary because by default the shell (the program that interprets what you type) only looks for programs in certain standard system directories. To make it run a program in the current directory, we have to include the directory name.
3. Some notes on what the program does
Noteworthy features of this program include:
The #include <stdio.h> in line 1. This is standard C boilerplate, and will appear in any program you see that does input or output. The meaning is to tell the compiler to include the text of the file /usr/include/stdio.h in your program as if you had typed it there yourself. This particular file contains declarations for the standard I/O library functions like puts (put string) and printf (print formatted), as used in the program. If you don't put it in, your program may or may not still compile. Do it anyway.
Lines 3 and 4 declare the main function. Every C program has to have a main function declared in exactly this way---it's what the operating system calls when you execute the program. The int on Line 3 says that main returns a value of type int (we'll describe this in more detail later), and that it takes two arguments: argc of type int, the number of arguments passed to the program from the command line, and argv, of a pointer type that we will get to eventually, which is an array of the arguments (essentially all the words on the command line, including the program name).
Everything inside the curly braces is the body of the main function. This includes
The declaration int i;, which says that i will be a variable that holds an int.
Line 8, which prints an informative message using puts.
The for loop on Lines 9-11, which executes its body for each value of i from 1 to 10. We'll explain how for loops work later. Note that the body of the loop is enclosed in curly braces just like the body of the main function. The only statement in the body is the call to printf on Line 10; this includes a format string that specifies that we want a decimal-formatted integer followed by a newline (the \n).
The return 0; on Line 13 tells the operating system that the program worked (the convention in Unix is that 0 means success). If the program didn't work for some reason, we could have returned something else to signal an error.
PineWiki