Section 11.3. Single-Module Programs


[Page 401]

11.3. Single-Module Programs

Let'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] Most of the examples used in this chapter are available online; see the Preface for more information.

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 
[Page 402]
38 after[len] = 0; /* terminate reversed string */ 39 }


11.3.1. Compiling a C Program

To 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:

  • The errors on lines 16 and 18 were due to inappropriate use of double quotes within double quotes.

  • The error on line 35 was due to an illegal use of a semicolon (;).

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 Program

Here 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 */ 
[Page 403]
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 38 after[len] = 0; /* terminate reversed string */ 39 }


11.3.3. Running a C Program

After 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 $ _ 



[Page 404]

11.3.4. Overriding the Default Executable Name

The 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 $ _ 





Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net