Computer Science 202
Systems Programming Concepts Using C
Lab 7 - Printing Function
Winter 1998


Write a C function that will print to a stream, a string within a specified field width.


Write a FUNCTION that has the following prototype:

int PrintString( FILE *fp, char string[ ], int width );

This function is to print to the specified stream a series of characters from the given string. The string is to be printed within the indicated field width. For example if the field width is 10 and the string is Fred then 6 blank spaces are printed and then the 4 characters from the string are printed. If the field width is -10 then the 4 characters from the string are printed followed by 6 blanks. If the number of characters in the string are greater than the indicated size of the field, then only print the first n characters. For example if the string was Michigan and the field width was 5 then the function would only print Michi. The return value for the function is the actual number of characters from the string that are printed, this does not include any blanks that are printed.

In order to test your function you must also write a main program that calls the function. Write a simple main program that asks the user for a string and a field width and prints the string to the screen.


The following must be delivered to your instructor via text-only email by the end of class on 8th Tuesday February 24, 1998.

  1. Source code for your program.
In addition, you must deliver to the instructor by the end of lab on 8th Tuesday, February 24, 1998 the following:
  1. A printed copy of your source code.


  1. Since this is an in-class lab only minimal commenting is required.
  2. Make sure that you name is on the file.
  3. Meaningful variable names are required.