Download the C compiler from here .download C compiler
and then run the following program than see the output.
The First Type & Run
Enter and compile the following program. If you get any errors, make sure you entered
the program correctly.
The usage for this program is print_it filename.ext, where filename.ext is the
source filename along with the extension. Note that this program adds line numbers to
the listing. (Don’t let this program’s length worry you; you’re not expected to understand
it yet. It’s included here to help you compare printouts of your programs with the ones
given in the book.)
LISTING T&R 1 print_it.c
1: /* print_it.c—This program prints a listing with line numbers! */
2: #include <stdlib.h>
3: #include <stdio.h>
4:
5: void do_heading(char *filename);
6:
7: int line = 0, page = 0;
8:
9: int main( int argv, char *argc[] )
10: {
11: char buffer[256];
12: FILE *fp;
13:
14: if( argv < 2 )
15: {
16: fprintf(stderr, “\nProper Usage is: “ );
17: fprintf(stderr, “\n\nprint_it filename.ext\n” );
18: return(1);
19: }
20:
21: if (( fp = fopen( argc[1], “r” )) == NULL )
22: {
23: fprintf( stderr, “Error opening file, %s!”, argc[1]);
24: return(1);
25: }
26:
27: page = 0;
28: line = 1;
29: do_heading( argc[1]);
30:
31: while( fgets( buffer, 256, fp ) != NULL )
32: {
33: if( line % 55 == 0 )
34: do_heading( argc[1] );
35:
26 Type & Run 1
Printing Your Listings 27
36: fprintf( stdprn, “%4d:\t%s”, line++, buffer );
37: }
38:
39: fprintf( stdprn, “\f” );
40: fclose(fp);
41: return 0;
42: }
43:
44: void do_heading( char *filename )
45: {
46: page++;
47:
48: if ( page > 1)
49: fprintf( stdprn, “\f” );
50:
51: fprintf( stdprn, “Page: %d, %s\n\n”, page, filename );
52: }
LISTING T&R 1 continued
This listing uses a value that is available within many PC compilers, but not
necessarily in all other compilers. Although stdout is an ANSI-defined value,
stdprn is not. You need to check your compiler for specifics on sending output
to the printer.
One option for getting around this is to change the stdprn statements to
stdout statements. This causes the output to go to the screen. Using your
operating system’s redirection features (or by piping if you’re using UNIX or
Linux), you should be able to redirect the output from the screen to the
printer.
Note
On Day 14, “Working with the Screen, Printer, and Keyboard,” you will learn more
about how this program works.
and then run the following program than see the output.
The First Type & Run
Enter and compile the following program. If you get any errors, make sure you entered
the program correctly.
The usage for this program is print_it filename.ext, where filename.ext is the
source filename along with the extension. Note that this program adds line numbers to
the listing. (Don’t let this program’s length worry you; you’re not expected to understand
it yet. It’s included here to help you compare printouts of your programs with the ones
given in the book.)
LISTING T&R 1 print_it.c
1: /* print_it.c—This program prints a listing with line numbers! */
2: #include <stdlib.h>
3: #include <stdio.h>
4:
5: void do_heading(char *filename);
6:
7: int line = 0, page = 0;
8:
9: int main( int argv, char *argc[] )
10: {
11: char buffer[256];
12: FILE *fp;
13:
14: if( argv < 2 )
15: {
16: fprintf(stderr, “\nProper Usage is: “ );
17: fprintf(stderr, “\n\nprint_it filename.ext\n” );
18: return(1);
19: }
20:
21: if (( fp = fopen( argc[1], “r” )) == NULL )
22: {
23: fprintf( stderr, “Error opening file, %s!”, argc[1]);
24: return(1);
25: }
26:
27: page = 0;
28: line = 1;
29: do_heading( argc[1]);
30:
31: while( fgets( buffer, 256, fp ) != NULL )
32: {
33: if( line % 55 == 0 )
34: do_heading( argc[1] );
35:
26 Type & Run 1
Printing Your Listings 27
36: fprintf( stdprn, “%4d:\t%s”, line++, buffer );
37: }
38:
39: fprintf( stdprn, “\f” );
40: fclose(fp);
41: return 0;
42: }
43:
44: void do_heading( char *filename )
45: {
46: page++;
47:
48: if ( page > 1)
49: fprintf( stdprn, “\f” );
50:
51: fprintf( stdprn, “Page: %d, %s\n\n”, page, filename );
52: }
LISTING T&R 1 continued
This listing uses a value that is available within many PC compilers, but not
necessarily in all other compilers. Although stdout is an ANSI-defined value,
stdprn is not. You need to check your compiler for specifics on sending output
to the printer.
One option for getting around this is to change the stdprn statements to
stdout statements. This causes the output to go to the screen. Using your
operating system’s redirection features (or by piping if you’re using UNIX or
Linux), you should be able to redirect the output from the screen to the
printer.
Note
On Day 14, “Working with the Screen, Printer, and Keyboard,” you will learn more
about how this program works.
Comments
Post a Comment