Home > Articles > Programming > C/C++

  • Print
  • + Share This
This chapter is from the book

A Review of the Parts of a Program

Now that all the parts of a program have been described, you can look at any program and find some similarities. Look at Listing 2.2 and see whether you can identify the different parts.

Input down-arrow.jpg

Listing 2.2 list_it.c – A Program to List a Code Listing with Added Line Numbers

 1:  /* list_it.c_ _This program displays a listing with line numbers! */
 2:  #include <stdio.h>
 3:  #include <stdlib.h>
 4:  #define BUFF_SIZE 256
 5:  void display_usage(void);
 6:  int line;
 7:
 8:  int main( int argc, char *argv[] )
 9:  {
10:    char buffer[BUFF_SIZE];
11:    FILE *fp;
12:
13:    if( argc < 2 )
14:    {
15:       display_usage();
16:       return (1);
17:    }
18:
19:    if (( fp = fopen( argv[1], "r" )) == NULL )
20:    {
21:         fprintf( stderr, "Error opening file, %s!", argv[1] );
22:         return(1);
23:    }
24:
25:    line = (1);
26:
27:    while( fgets( buffer, BUFF_SIZE, fp ) != NULL )
28:       fprintf( stdout, "%4d:\t%s", line++, buffer );
29:
30:    fclose(fp);
31:    return 0;
32: }
33:
34: void display_usage(void)
35: {
36:       fprintf(stderr, "\nProper Usage is: " );
37:       fprintf(stderr, "\n\nlist_it filename.ext\n" );
38: }

Output down-arrow.jpg

C:\>list_it list_it.c
 1:   /* list_it.c - This program displays a listing with line numbers! */
 2:   #include <stdio.h>
 3:   #include <stdlib.h>
 4:   #define BUFF_SIZE 256
 5:   void display_usage(void);
 6:   int line;
 7:
 8:   int main( int argc, char *argv[] )
 9:   {
10:       char buffer[BUFF_SIZE];
11:       FILE *fp;
12:
13:       if( argc < 2 )
14:       {
15:            display_usage();
16:            return (1);
17:       }
18:
19:       if (( fp = fopen( argv[1], "r" )) == NULL )
20:       {
21:            fprintf( stderr, "Error opening file, %s!", argv[1] );
22:            return(1);
23:       }
24:
25:       line = 1;
26:
27:       while( fgets( buffer, BUFF_SIZE, fp ) != NULL )
28:          fprintf( stdout, "%4d:\t%s", line++, buffer );
29:
30:       fclose(fp);
31:       return (0);
32:  }
33:
34:  void display_usage(void)
35:  {
36:       fprintf(stderr, "\nProper Usage is: " );
37:       fprintf(stderr, "\n\nlist_it filename.ext\n" );
38:  }

Analysis down-arrow.jpg

The list_it.c program in Listing 2.2 displays C program listings that you have saved. These listings display on the screen with line numbers added.

Looking at this listing, you can summarize where the different parts are. The required main() function is in lines 8 through 32. Lines 2 and 3 have #include directives. Lines 6, 10, and 11 have variable definitions. Line 4 defines a constant BUFF_SIZE as 256, the stand size for buffers. The value to doing this is that if the buffer size changes, you only need to adjust this one line and all lines using this constant will automatically update. If you hardcode a number like 256, you’d have to search all your lines of code to make sure you caught all mentions.

A function prototype, void display_usage(void), is in line 5. This program has many statements (lines 13, 15, 16, 19, 21, 22, 25, 27, 28, 30, 31, 36, and 37). A function definition for display_usage() fills lines 34 through 38. Braces enclose blocks throughout the program. Finally, only line 1 has a comment. In most programs, you should probably include more than one comment line.

list_it.c calls many functions. It calls only one user-defined function, display_usage(). The library functions that it uses are fopen() in line 19; fprintf() in lines 21, 28, 36, and 37; fgets() in line 27; and fclose() in line 30. These library functions are covered in more detail throughout this book.

  • + Share This
  • 🔖 Save To Your Account