-->


- 27 -

Programming in C

by Rick McMullin

IN THIS CHAPTER

Linux is distributed with a wide range of software-development tools. Many of these tools support the development of C and C++ applications. This chapter describes the tools that can be used to develop and debug C applications under Linux. It is not intended to be a tutorial on the C programming language, but rather to describe how to use the C compiler and some of the other C programming tools that are included with Linux. In this chapter you will learn about the following:

You also will look at some of the useful C tools that are included with the Linux distribution. These tools include pretty print programs, additional debugging tools, and automatic function prototypers.


NOTE: Pretty print programs are programs that automatically reformat code so that it has consistent indenting.

What Is C?

C is a general-purpose programming language that has been around since the early days of the UNIX operating system. It was originally created by Dennis Ritchie at Bell Laboratories to aid in the development of UNIX. The first versions of UNIX were written using assembly language and a language called B. C was developed to overcome some of the shortcomings of B. Since that time, C has become one of the most widely used computer languages in the world.

Why did C gain so much support in the programming world? Some of the reasons that C is so commonly used include the following:

C has evolved quite a bit over the last 20 years. In the late 1980s, the American National Standards Institute published a standard for the C language known as ANSI C. This further helped to secure C's future by making it even more consistent between platforms. The 1980s also saw an object-oriented extension to C called C++. C++ will be described in the next chapter, "Programming in C++."

The C compiler that is available for Linux is the GNU C compiler, abbreviated GCC. This compiler was created under the Free Software Foundation's programming license and is therefore freely distributable. You will find it on this book's companion CD-ROM.

The GNU C Compiler

The GNU C Compiler (GCC) that is packaged with the Slackware Linux distribution is a fully functional, ANSI C compatible compiler. If you are familiar with a C compiler on a different operating system or hardware platform, you will be able to learn GCC very quickly. This section describes how to invoke GCC and introduces many of the commonly used GCC compiler options.

Invoking GCC

The GCC compiler is invoked by passing it a number of options and a number of filenames. The basic syntax for invoking gcc is this:

gcc [options] [filenames]



The operations specified by the command-line options will be performed on each of the files that are specified on the command line. The next section describes the options that you will use most often.

GCC Options

There are more than 100 compiler options that can be passed to GCC. You will probably never use many of these options, but you will use some of them on a regular basis. Many of the GCC options consist of more than one character. For this reason you must specify each option with its own hyphen, and you cannot group options after a single hyphen as you can with most Linux commands. For example, the following two commands are not the same:

gcc -p -g test.c



gcc -pg test.c



The first command tells GCC to compile test.c with profile information for the prof command and also to store debugging information within the executable. The second command just tells GCC to compile test.c with profile information for the gprof command.

When you compile a program using gcc without any command-line options, it will create an executable file (assuming that the compile was successful) and call it a.out. For example, the following command would create a file named a.out in the current directory.

gcc test.c



To specify a name other than a.out for the executable file, you can use the -o compiler option. For example, to compile a C program file named count.c into an executable file named count, you would type the following command.

gcc -o count count.c




NOTE: When you are using the -o option, the executable filename must occur directly after the -o on the command line.

There are also compiler options that allow you to specify how far you want the compile to proceed. The -c option tells GCC to compile the code into object code and to skip the assembly and linking stages of the compile. This option is used quite often because it makes the compilation of multifile C programs faster and easier to manage. Object code files that are created by GCC have a .o extension by default.

The -S compiler option tells GCC to stop the compile after it has generated the assembler files for the C code. Assembler files that are generated by GCC have a .s extension by default. The -E option instructs the compiler to perform only the preprocessing compiler stage on the input files. When this option is used, the output from the preprocessor is sent to the standard output rather than being stored in a file.

Optimization Options

When you compile C code with GCC, it tries to compile the code in the least amount of time and also tries to create compiled code that is easy to debug. Making the code easy to debug means that the sequence of the compiled code is the same as the sequence of the source code, and no code gets optimized out of the compile. There are many options that you can use to tell GCC to create smaller, faster executable programs at the cost of compile time and ease of debugging. Of these options the two that you will typically use are the -O and the -O2 options.

The -O option tells GCC to perform basic optimizations on the source code. These optimizations will in most cases make the code run faster. The -O2 option tells GCC to make the code as fast and small as it can. The -O2 option will cause the compilation speed to be slower than it is when using the -O option, but will typically result in code that executes more quickly.

In addition to the -O and -O2 optimization options, there are a number of lower-level options that can be used to make the code faster. These options are very specific and should only be used if you fully understand the consequences that using these options will have on the compiled code. For a detailed description of these options, refer to the GCC manual page by typing man gcc on the command line.

Debugging and Profiling Options

GCC supports several debugging and profiling options. Of these options, the two that you are most likely to use are the -g option and the -pg option.

The -g option tells GCC to produce debugging information that the GNU debugger (gdb) can use to help you to debug your program. GCC provides a feature that many other C compilers do not have. With GCC you can use the -g option in conjunction with the -O option (which generates optimized code). This can be very useful if you are trying to debug code that is as close as possible to what will exist in the final product. When you are using these two options together you should be aware that some of the code that you have written will probably be changed by GCC when it optimizes it. For more information on debugging your C programs, refer to the "Debugging GCC Programs with gdb" section in this chapter.

The -pg option tells GCC to add extra code to your program that will, when executed, generate profile information that can be used by the gprof program to display timing information about your program. For additional information on gprof, refer to the "gprof" section in this chapter.

Debugging GCC Programs with gdb

Linux includes the GNU debugging program called gdb. gdb is a very powerful debugger that can be used to debug C and C++ programs. It enables you to see the internal structure or the memory that is being used by a program while it is executing. Some of the functions that gdb provides for you are these:

You can run gdb by typing gdb on the command line and pressing Enter. If your system is configured properly, gdb should start and you will see a screen that resembles the following:

GDB is free software and you are welcome to distribute copies of it



under certain conditions; type "show copying" to see the conditions.



There is absolutely no warranty for GDB; type "show warranty" for details.



GDB 4.14 (i486-slakware-linux), Copyright 1995 Free Software Foundation, Inc.



(gdb)



When you start gdb, there are a number of options that you can specify on the command line. You will probably run gdb in the following way:

gdb <fname>



When you invoke gdb in this way, you are specifying the executable file that you want to debug. This tells gdb to load the executable file with the name fname. There are also ways of starting gdb that tell it to inspect a core file that was created by the executable file being examined, or to attach gdb to a currently running process. To get a listing and brief description of each of these other options, you can refer to the gdb man page or type gdb -h at the command line.

Compiling Code for Debugging

To get gdb to work properly, you must compile your programs so that debugging information will be generated by the compiler. The debugging information that is generated contains the types for each of the variables in your program as well as the mapping between the addresses in the executable program and the line numbers in the source code. gdb uses this information to relate the executable code to the source code.

To compile a program with the debugging information turned on, use the -g compiler option.

gdb Basic Commands

The gdb supports many commands that enable you to perform different debugging operations. These commands range in complexity from very simple file-loading commands to complicated commands that allow you to examine the contents of the call stack. Table 27.1 describes the commands that you will need to get up and debugging with gdb. To get a description of all of the gdb commands, refer to the gdb manual page.

Table 27.1. Basic gdb commands.
Command Description
file Loads the executable file that is to be debugged
kill Terminates the program that you are currently debugging
list Lists sections of the source code used to generate the executable file
next Advances one line of source code in the current function, without stepping into other functions
step Advances one line of source code in the current function, and does step into other functions
run Executes the program that is currently being debugged
quit Terminates gdb
watch Enables you to examine the value of a program variable whenever the value changes
break Sets a breakpoint in the code; this causes the execution of the program to be suspended whenever this point is reached
make Enables you to remake the executable program without quitting gdb or using another window
shell Enables you to execute UNIX shell commands without leaving gdb

The gdb environment supports many of the same command-editing features as the UNIX shell programs do. You can tell gdb to complete unique commands by pressing the Tab key just as you do when you are using bash or tcsh. If what you have typed in is not unique, you can make gdb print a list of all the commands that match what you have typed in so far by pressing the Tab key again. You can also scroll up and down through the commands that you have entered previously by pressing the up and down arrow keys.

Sample gdb Session

This section takes you step by step through a sample gdb session. The sample program that is being debugged is quite simple, but it is sufficient to illustrate how gdb is typically used.

We will start by showing a listing of the program that is to be debugged. The program is called greeting and is supposed to display a simple greeting followed by the greeting printed in reverse order.

#include  <stdio.h>







main ()



{



  char my_string[] = "hello there";







  my_print (my_string);



  my_print2 (my_string);



}







void my_print (char *string)



{



  printf ("The string is %s\n", string);



}







void my_print2 (char *string)



{



  char *string2;



  int size, i;







  size = strlen (string);



  string2 = (char *) malloc (size + 1);



  for (i = 0; i < size; i++)



    string2[size - i] = string[i];



  string2[size+1] = `\0';



  printf ("The string printed backward is %s\n", string2);



}



You can compile the preceding program using the gcc command followed by the filename. If you want to rename the generated binary (instead of using the default a.out filename), use the -o option followed by the binary name, like this:

gcc -o test test.c



The program, when executed, displays the following output:

The string is hello there



The string printed backward is



The first line of output comes out correctly, but the second line prints something that was unexpected. We intended the second line of output to be

The string printed backward is ereht olleh



For some reason the my_print2 function is not working properly. Let's take a look at the problem using gdb. First you need to start gdb, specifying the greeting program as the one to debug. You do this by typing the following command:

gdb greeting




NOTE: Remember that you must compile the greeting program with the compiler debug options turned on.

If you forget to pass the program to debug as a parameter to gdb, you can load it in after gdb is started by using the file command at the gdb prompt:

(gdb) file greeting



This command will load the greeting executable just as if you had told gdb to load it on the command line.

You can now run greeting by entering the gdb run command. When the program is executed from within gdb, the result should resemble the following:

(gdb) run



Starting program: /root/greeting



The string is hello there



The string printed backward is



Program exited with code 041



The output of the greeting program is the same as when we executed the program outside of gdb. The question is, why is the backward print not working? To find the problem we can set a breakpoint at the line after the for statement in the my_print2 function. To do this, list the source file by entering the list command three times at the gdb prompt:

(gdb) list



(gdb) list



(gdb) list




TIP: Pressing Enter by itself at the gdb prompt will repeat the last command that was entered.

The first time you enter the list command, you get output that resembles the following:

1       #include  <stdio.h>



2



3       main ()



4       {



5         char my_string[] = "hello there";



6



7         my_print (my_string);



8         my_print2 (my_string);



9       }



10



If you press Enter, gdb will execute the list command again, giving you the following output:

11      my_print (char *string)



12      {



13        printf ("The string is %s\n", string);



14      }



15



16      my_print2 (char *string)



17      {



18        char *string2;



19        int size, i;



20



Pressing Enter one more time will list the rest of the greeting program:

21        size = strlen (string);



22        string2 = (char *) malloc (size + 1);



23        for (i = 0; i < size; i++)



24          string2[size - i] = string[i];



25        string2[size+1] = `\0';



26        printf ("The string printed backward is %s\n", string2);



27      }



By listing the file you can see that the place where you want to set the breakpoint is line 24. Now, to set the breakpoint, type the following command at the gdb command prompt:

(gdb) break 24



gdb should now print a response resembling the following:

Breakpoint 1 at 0x139: file greeting.c, line 24



(gdb)



Now you can run the program again by typing the run command. This command will generate the following output:

Starting program: /root/greeting



The string is hello there







Breakpoint 1, my_print2 (string = 0xbfffdc4 "hello there") at greeting.c :24



24  string2[size-i]=string[i]



You can see what is actually going wrong with your program by setting a watch to tell you the value of the string2[size - i] variable expression.

To do this, type

(gdb) watch string2[size - i]



gdb will return the following acknowledgment:

Watchpoint 2: string2[size - i]



The version of gdb included on the CD-ROM accompanying this book changes the prompt to Hardware Watchpoint, but this is simply a change in the naming convention used by gdb. Now you can step through the execution of the for loop using the next command:

(gdb) next



After the first time through the loop, gdb tells us that string2[size - i] is `h'. gdb informs you of this by writing the following message on the screen:

Watchpoint 2, string2[size - i]



Old value = 0 `\000'



New value = 104 `h'



my_print2(string = 0xbfffdc4 "hello there") at greeting.c:23



23 for (i=0; i<size; i++)




NOTE: The version of gcc on the CD-ROM accompanying this book appears to have some bugs in the include files. Some tasks, including checking the next watchpoint, cause error messages. You should check the FTP sites for updates or previous versions which do not have this fault if this presents a problem for your C compiler usage.

This is the value that you expected. Stepping through the loop several more times reveals similar results. Everything appears to be functioning normally. When you get to the point where i=10, the value of the string2[size - i] expression is equal to `e', the value of the size - i expression is equal to 1, and the program is at the last character that is to be copied over into the new string.

If you step through the loop one more time, you see that there was not a value assigned to string2[0], which is the first character of the string. Because the malloc function initializes the memory it assigns to null, the first character in string2 is the null character. This explains why nothing was being printed when you tried to print string2.

Now that you have found the problem, it should be quite easy to fix. You must write the code so that the first character going into string2 is being put into string2 at offset size - 1 instead of string2 at offset size. This is because the size of string2 is 12, but it starts numbering at offset zero. The characters in the string should start at offset 0 and go to offset 10, with offset 11 being reserved for the null character.

There are many ways to modify this code so that it will work. One way is to keep a separate size variable that is one smaller than the real size of the original string. This solution is shown in the following code:

#include  <stdio.h>







main ()



{



  char my_string[] = "hello there";







  my_print (my_string);



  my_print2 (my_string);



}







my_print (char *string)



{



  printf ("The string is %s\n", string);



}







my_print2 (char *string)



{



  char *string2;



  int size, size2, i;







  size = strlen (string);



  size2 = size -1;



  string2 = (char *) malloc (size + 1);



  for (i = 0; i < size; i++)



    string2[size2 - i] = string[i];



  string2[size] = `\0';



  printf ("The string printed backward is %s\n", string2);



}



Additional C Programming Tools

The Slackware Linux distribution includes a number of C development tools that have not yet been described. This section describes many of these additional tools and their typical uses.

xxgdb

xxgdb is an X Window system-based graphical user interface to gdb. All of the features that exist in the command-line version of gdb are present in xxgdb. xxgdb enables you to perform many of the most commonly used gdb commands by pressing buttons instead of typing commands. It also graphically represents where you have placed breakpoints.

You can start xxgdb by typing the following into an Xterm window.

xxgdb



When you initiate xxgdb, you can specify any of the command-line options that were available with gdb. The xxgdb program also has some of its own command-line options. These are described in Table 27.2.

Table 27.2. The xxgdb command-line options.
Option Description
db_name Specifies the name of the debugger to be used. The default is gdb.
db_prompt Specifies the debugger prompt. The default is gdb.
gdbinit

Specifies the filename of the initial gdb command file. The default is .gdbinit.

nx Tells xxgdb not to execute the .gdbinit file.
bigicon Uses a large icon size for the xxgdb icon.


When you start xxgdb, a window opens on your screen. This window is shown in Figure 27.1.

The xxgdb main window contains a message that is similar to the one displayed on the screen when you start the command-line version of gdb. Near the bottom of the xxgdb main window there are four buttons. The Source Listing, Command Buttons, and Display Window buttons each bring up an additional window when they are activated. The Quit button terminates the xxgdb program.

FIGURE 27.1. The xxgdb main window.

The Source Listing button brings up a window that will display the source code for the program that is being debugged. This window is shown in Figure 27.2.

The Command Buttons button brings up a window that contains 26 other buttons. These buttons each execute a gdb command. The gdb command-button window is illustrated in Figure 27.3.

The Display Window button brings up a window that is used to display the results of any display command. You can tell xxgdb what variable or expression to display by selecting it from the source listing and then clicking on the Display button in the command-button window. The display window is illustrated in Figure 27.4.

For more information on using xxgdb, refer to the xxgdb manual page and the gdb manual page.

FIGURE 27.2.The xxgdb source listing window.

FIGURE 27.3. The xxgdb command-button window.

FIGURE 27.4. The xxgdb display window.

calls

calls is a program that is not included on the Linux CD-ROM accompanying this book, but you can obtain a copy from the sunsite.unc.edu FTP site under the directory /pub/Linux/devel/lang/c/calls.tar.Z. Some older CD-ROM distributions of Linux include this file. Because it is a useful tool, we will cover it here. If you think it will be of use to you, obtain a copy from an FTP or BBS site or another CD-ROM. calls runs the GCC preprocessor on the files that are passed to it on the command line, and displays a function call tree for the functions that are in those files.


NOTE: To install calls on your system, perform the following steps while you are logged in as root: 1. Uncompress and untar the file. 2. cd into the calls subdirectory that was created by untarring the file. 3. Move the file named calls to the /usr/bin directory. 4. Move the file named calls.1 to the /usr/man/man1 directory. 5. Remove the /tmp/calls directory. This will install the calls program and man page on your system.

When calls prints out the call trace, it includes the filename in which the function was found in brackets after the function name:

main [test.c]



If the function was not in one of the files that was passed to calls, it does not know where that function lives and prints only the function name:

printf



calls also makes note of recursive and static functions in its output. Recursive functions are represented in the following way:

fact <<< recursive in factorial.c >>>



Static functions are represented like this:

total [static in calculate.c]



As an example, assume that you executed calls with the following program as input:

##include <stdio.h>







main ()



{



char my_string[] = "hello there";



my_print (my_string);



my_print2(my_string);



}







my_print (char *string)



{



printf ("The string is %s\n", string);



}







my_print2 (char *string)



{



  char *string2;



  int size, size2, i;







  size = strlen (string);



  size2 = size -1;



  string2 = (char *) malloc (size + 1);



  for (i = 0; i < size; i++)



    string2[size2 - i] = string[i];



  string2[size] = `\0';



  printf ("The string printed backward is %s\n", string2);



}



This would generate the following output:

    1 main [test.c]



    2       my_print [test.c]



    3             printf



    4       my_print2 [test.c]



    5             strlen



    6             malloc



    7             printf




WARNING: The version of gcc included with this book's CD-ROM may generate error messages running this code. Again, you may want to check FTP sites for a more recent version or bug fixes, or revert to an older compiler version.

calls recognizes a number of command-line options that enable you to specify the appearance of the output and what function calls get displayed. For more information on these command-line options, refer to the calls manual page or type calls -h at the command line.

cproto

cproto is another program that is not included on this Linux CD-ROM but is readily available from FTP and BBS sites. cproto reads in C source files and automatically generates function prototypes for all of the functions. Using cproto saves you from having to type in a function definition for all of the functions that you have written in your programs.


NOTE: To install cproto on your system, perform the following steps while you are logged in as root: 1. Uncompress and untar the file. 2. cd into the cproto subdirectory that was created by untarring the file. 3. Move the file named cproto to the /usr/bin directory. 4. Move the file named cproto.1 to the /usr/man/man1 directory. 5. Remove the /tmp/cproto directory. This will install the cproto program and man page on your system.

If you ran the following code through the cproto program

#include  <stdio.h>







main ()



{



  char my_string[] = "hello there";



  my_print (my_string);



  my_print2(my_string);



}







my_print (char *string)



{



  printf ("The string is %s\n", *string);



}







my_print2 (char *string)



{



  char *string2;



  int size, size2, i;







  size = strlen (string);



  size2 = size -1;



  string2 = (char *) malloc (size + 1);



  for (i = 0; i < size; i++)



    string2[size2 - i] = string[i];



  string2[size] = `\0';



  printf ("The string printed backward is %s\n", string2);



}



you would get the following output:

/* test.c */



int main(void);



int my_print(char *string);



int my_print2(char *string);




WARNING: As with the calls program, discussed earlier, the version of gcc included on this book's CD-ROM may cause errors when compiling this program. You should obtain a bug fix (if one is available) or revert to an earlier version of the compiler to avoid these problems.

This output could be redirected to an include file and used to define the prototypes for all of the functions.

indent

The indent utility is another programming utility that is included with Linux. This program, in its simplest form, reformats or pretty prints your C code so that it is consistently indented and all opening and closing braces are represented consistently. There are numerous options that enable you to specify how you want indent to format your code. For information on these options, refer to the indent manual page or type indent -h at the command line.

The following example shows the default output of the indent program.

C code before running indent:

#include  <stdio.h>







main () {



      char my_string[] = "hello there";



  my_print (my_string);



     my_print2(my_string); }







my_print (char *string)



{



  printf    ("The string is %s\n", *string);



}







my_print2           (char *string) {



    char *string2;



      int size, size2, i;







      size = strlen (string);



      size2 = size -1;



      string2 = (char *) malloc (size + 1);



  for (i = 0; i < size; i++)



            string2[size2 - i] = string[i];



      string2[size] = `\0';



      printf ("The string printed backward is %s\n", string2);



}



C code after running indent:

#include  <stdio.h>







main ()



{



  char my_string[] = "hello there";



  my_print (my_string);



  my_print2 (my_string);



}







my_print (char *string)



{



  printf ("The string is %s\n", *string);



}







my_print2 (char *string)



{



  char *string2;



  int size, size2, i;







  size = strlen (string);



  size2 = size -1;



  string2 = (char *) malloc (size + 1);



  for (i = 0; i < size; i++)



    string2[size2 - i] = string[i];



  string2[size] = `\0';



  printf ("The string printed backward is %s\n", string2);



}



Indent does not change how the code compiles; it just changes how the source code looks. It makes the code more readable, which is always a good thing.

gprof

gprof is a program that is installed in the /usr/bin directory on your Linux system. It allows you to profile programs that you write to determine where most of the execution time is being spent.

gprof will tell you how many times each function that your program uses is called, and also the percentage of the total execution time the program spent in each function. This information can be very useful if you are trying to improve the performance of a program.

To use gprof on one of your programs, you must compile the program using the -pg gcc option. This causes the program to create a file called gmon.out each time it is executed. gprof uses the gmon.out file to generate the profile information.

After you run your program and it has created the gmon.out file, you can get its profile by entering the following command:

gprof <program_name>



The program_name parameter is the name of the program that created the gmon.out file.


TIP: The profile data that gprof displays to the screen is quite large. If you want to examine this data, you should redirect gprof's output to a file.

f2c and p2c

f2c and p2c are two source code conversion programs. f2c converts FORTRAN code into C code, and p2c converts Pascal code into C code. Both are included in the Linux installation when you install GCC.

If you have some code that has been written using either FORTRAN or Pascal that you want to rewrite in C, f2c and p2c can prove to be very useful programs. Both programs produce C code that can typically be compiled directly by GCC without any human intervention.

If you are converting small, straightforward FORTRAN or Pascal programs, you should be able to get away with using f2c or p2c without any options. If you are converting very large programs consisting of many files, you will probably have to use some of the command-line options that are provided by the conversion program that you are using.

To invoke f2c on a FORTRAN program, enter the following command:

f2c my_fortranprog.f




NOTE: f2c requires that the program being converted has either a .f or a .F extension.

To convert a Pascal program to C, enter the following command:

p2c my_pascalprogram.pas



Both of these commands create C source code files that have the same name as the original file, except with a .c extension instead of .f or .pas.

For more information on the specific conversion options that are available with f2c or p2c, refer to their respective man pages.

Summary

This chapter introduced the GNU C compiler and many of the options that you will typically use when you compile C code. It also introduced the concepts behind debugging code with the GNU debugger, and illustrated the usefulness of some of the other C utility programs that are included on the Linux CD-ROM.


TIP: If you will be writing C code, the time that you spend learning how to use gdb and some of the other tools mentioned in this chapter will be more than worth your effort in light of the time you will save later.

The next chapter will discuss many of the same topics, but with a focus on C++ development rather than C development.