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:
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.
Line 3 is a comment; its beginning and end is marked by the /* and */ characters. Comments are ignored by the compiler but can be helpful for other programmers looking at your code (including yourself, after you've forgotten why you wrote something).
Lines 5 and 6 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 in C/Functions), 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 (C/Pointers), which is an array of the arguments (essentially all the words on the command line, including the program name). Note that it would also work to do this as one line (as KerniganRitchie typically does); the C compiler doesn't care about whitespace, so you can format things however you like, subject to the constraint that consistency will make it easier for people to read your code.
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 (C/IntegerTypes).
Line 10, which prints an informative message using puts (C/InputOutput).
The for loop on Lines 11–13, which executes its body for each value of i from 1 to 10. We'll explain how for loops work later (C/Statements). 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 12; 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 15 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.