CSC 173, Fall 2001

Assignment 2: C

This assignment consists of two written questions and two programming questions. For the programming assignments, please use the gnu (Free Software Foundation) C compiler, gcc. To help with debugging, use the gnu debugger, gdb.

Written questions

  1. If you declare a local variable that's a C array, you have to specify the number of elements in the array. On the other hand, if you declare a function parameter that's a C array, you don't have to specify the number of elements, e.g.
          void foo (int parameter_array[])
              int local_array[10];
    Why the difference?

  2. What does this program print in C?
          struct integer {
              int val;
          static void bar(struct integer i) {
              i.val = 3;
          main() {
              struct integer n;
              n.val = 2;
              printf("%d\n", n.val);
    What does this similar program print in Java?
          class integer {
              public int val = 0;
          public class num {
          static void bar(integer i) {
              i.val = 3;
          public static void main(String[] args) {
              integer n = new integer();
              n.val = 2;
    Why the difference? How could you modify the C program to make it behave like the Java one?

Programming questions

  1. Write a C program that will echo standard input to standard output, one line at a time. Your code must include the following lines:
          #include <stdio.h>
          void get_line (char *s)
              /* your code here */
          void put_line (char *s)
              /* your code here */
          main () {
              char s[100];
              while (1) {
                  if (!s[0]) break;
    You should modify only the places where /* your code here */ appears. Your code for get_line and put_line should duplicate the functionality of the standard gets and puts library routines, except that (1) you need not return a useful value, and (2) get_line should not strip the newline characters off the end of its input lines, and put_line should not add a newline to its output lines. (This behavior with respect to newlines is the same as displayed by the file-specific fgets and fputs routines.)

    Within get_line and put_line you should use the getchar and putchar library routines. You are not permitted (for this one assignment) to use gets, puts, scanf, or printf.

    Your code must reside in a file named parrot.c in a directory named parrot, which must be an immediate subdirectory of the directory in which you run the turnin script.

    Finally, in your README.txt or README.pdf file, you must answer the following questions: What may happen if you feed your parrot program input containing a line longer than 100 characters? What might you do about this problem?

  2. Building upon your solution to the previous question, write a C program that will print its input lines in reverse order. That is, if the input contains N lines, the program will print line N, followed by line N-1, etc., down through line 1. The characters within any individual line will be printed in the original order. (In other words, you should duplicate the behavior of the standard utility tail when run with the -r command-line switch.)

    You should store your strings internally in a linked list. You may place a (large) fixed limit on the length of an input line, but you should try not to use that much space for every line: once read into memory, a line should be copied to dynamically allocated space of the appropriate size. You code should handle an arbitrary number of input lines. You may use any of the functions in the <string.h> library package, with the exception of strdup; type "man 3 string" to learn more.

    Prior to terminating, your program must deallocate (free) any dynamically allocated (malloced) space. (Your program will appear to run correctly even if you don't, but your code would then "leak storage" if used as part of a larger system. The TAs will be checking for proper memory cleanup.

    Your code must reside in a directory named reverselines, which must be an immediate subdirectory of the directory in which you run the turnin script.

What to hand in

Answers to the written questions and documentation for the programming questions must appear in your README.txt or README.pdf file. This file must be located in the directory in which you run the turnin script. The only other things in this directory should be the two subdirectories parrot and reverselines.

DUE DATE: 11:59 pm, Monday October 1.


There is no "trivia" assignment for this project. Watch the newsgroup for possible updates.

Back to the grading page

Back to the course home page

Last Change: 18 September 2001 / Michael Scott's email address