CSC 173, Fall 2002

Assignment 2: C

DUE DATE: 11:59 pm, Monday September 30.

NO EXTENSIONS.


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?
          #include "stdio.h"
     
          struct integer {
              int val;
          };
      
          static void bar(struct integer i) {
              i.val = 5;
          }
      
          main(int argc, char *argv[]) {
              struct integer n;
              n.val = 1;
              bar(n);
              printf("%d\n", n.val);
          }
      
    What does this similar program print in Java?
          import java.io.*;
      
          class integer {
              public int val = 0;
          }
      
          public class num {
      
          static void bar(integer i) {
              i.val = 5;
          }
      
          public static void main(String[] args) {
              integer n = new integer();
              n.val = 1;
              bar(n);
              System.out.println(n.val);
          }
          }
      
    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) {
                  get_line(s);
                  if (!s[0]) break;
                  put_line(s);
              }
          }
      
    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.)

    Your program should terminate when the user types CTRL-D as the first character of the input line.

    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 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.

    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 strdup" or "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 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. Remember, the README file must contain the usual information, as described in the General Project information. Finally, comment your code and use good coding style. The TAs will be looking for this.