11.3. Single-Module ProgramsLet's examine a C program [1] that performs a simple task: reversing a string. To begin with, I'll show you how to write, compile, link, and execute a program that solves the problem using a single source file. Then I'll explain why it's better to split the program up into several independent modules, and show you how to do this. Here's a source code listing of the first version of the reverse program:
1 /* REVERSE.C */ 2 3 #include <stdio.h> 4 5 /* Function Prototype */ 6 int reverse (); 7 8 /****************************************************************/ 9 10 main () 11 12 { 13 char str [100]; /* Buffer to hold reversed string */ 14 15 reverse ("cat", str); /* Reverse the string "cat" */ 16 printf ("reverse ("cat") = %s\n", str); /* Display */ 17 reverse ("noon", str); /* Reverse the string "noon" */ 18 printf ("reverse ("noon") = %s\n", str); /* Display */ 19 } 20 21 /****************************************************************/ 22 23 reverse (before, after) 24 25 char *before; /* A pointer to the source string */ 26 char *after; /* A pointer to the reversed string */ 27 28 { 29 int i; 30 int j; 31 int len; 32 33 len = strlen (before); 34 35 for (j = len - 1; i = 0; j >= 0; j--; i++) /* Reverse loop */ 36 after[i] = before[j]; 37 11.3.1. Compiling a C ProgramTo create and run the "reverse" program, I first created a subdirectory called "reverse" inside my home directory and then created the file "reverse.c" using the emacs editor. I then compiled the C program using the gcc utility. To prepare an executable version of a single, self-contained program, follow gcc by the name of the source code file, which must end in a ".c" suffix. gcc doesn't produce any output when the compilation is successful and it has no diagnostic comments. By default, gcc creates an executable file called "a.out" in the current directory. To run the program, type "a.out" (or "./a.out" if "." is not in your search path). Any errors that are encountered are sent to the standard error channel, which is connected by default to your terminal's screen. Here's what happened: $ mkdir reverse ...create subdirectory for source code. $ cd reverse $ ... I created the above file in r.c using emacs. $ gcc r.c ...compile source. r.c: In function 'main': r.c:16: error: parse error before "cat" r.c:18: error: parse error before "noon" r.c: In function 'reverse': r.c:35: error: parse error before ')' token $ _ As you can see, gcc found a number of compile-time errors:
Since these errors were easy to correct, I copied the error-laden "r.c" file to a file called "r.old1.c" and then removed the compile-time errors using emacs. I left the original file in the directory so that I could see the evolution of my programming attempts. 11.3.2. A Listing of the Corrected Reverse ProgramHere is the corrected version of the reverse program. The lines containing the errors that I corrected are in italics: 1 /* REVERSE.C */ 2 3 #include <stdio.h> 4 5 /* Function Prototype */ 11.3.3. Running a C ProgramAfter compiling the second version, which I'll call "rfix.c", I ran it by typing the name of the executable file, "a.out". As you can see, the answers were correct: $ gcc rfix.c ...compile source. $ ls -lG rfix.c a.out ...list file information. -rwxr-xr-x 1 ables 24576 Jan 5 16:16 a.out* -rw-r--r-- 1 ables 439 Jan 5 16:15 rfix.c $ ./a.out ...run the program. reverse ("cat") = tac reverse ("noon") = noon $ _ 11.3.4. Overriding the Default Executable NameThe name of the default executable, "a.out", is rather cryptic, and an "a.out" file produced by a subsequent compilation of a different program would overwrite the one that I just produced. To avoid both problems, it's best to use the -o option with gcc, which allows you to specify the name of the executable file that you wish to create: $ gcc rfix.c -o rfix ...call the executable "rfix". $ ls -lGF rfix -rwxr-xr-x 1 ables 24576 Jan 5 16:19 rfix* $ ./rfix ...run the executable "rfix". reverse ("cat") = tac reverse ("noon") = noon $ _ |