CS 2213/2211
 Advanced Programming
 Spring 2005

 Recitation 7:  Arrays of Strings
    Week 7: Mar 1-3
 Due (on time): 2005-03-08  23:59:59
 Due (late):        2005-03-13  23:59:59

Recitation 7 should be submitted following directions at: submissions with deadlines
  • 2005-03-08  23:59:59 (that's Tuesday, 8 March 2005, 11:59:59 pm) for full credit.
  • 2005-03-13  23:59:59 (that's Sunday, 13 March 2005, 11:59:59 pm) for 75% credit.


Introduction: This recitation works on areas:


Arrays of Strings: We have covered some of this material in class. Please consult the following pages:

  1. Array of Strings (Never created! See Weiss, Section 8.14.).
  2. Printing/creating Arrays of Strings.

C command line arguments: In C, the main function can have parameters, which give any arguments on the Unix command line as the C program is executed. These arguments are any characters at all separated by white space. C arranges these arguments into an array of strings, with a NULL terminator. These parameters can have two forms:

Of course you can use other variable names besides argc and argv.

Inside the main function, the variable argc gives the number of command line arguments, that is, the number of elements in the array of strings, not including the NULL at the end. argv gives the command line arguments themselves as an array of strings. (Note that Java doesn't have the first parameter because Java arrays know how big they are. Java can't use the NULL at the end, because Java doesn't allow pointer arithmetic.)


A. Printing command line arguments: For the first part of the recitation, write a C program that will print its command line arguments with the following requirements:


Self-reproducing C programs: One can write a C program that will print exactly its own source as its output. This is not of particular technical interest, since if one wants to duplicate a C program running under Unix, a better and simpler way would just use some "duplicate" system call included in the C source. Nevertheless, these self-reproducing C programs are amusing and instructive, since at first sight the problem of writing such a program seems impossible.

I have provided three web pages showing and describing self-reproducing programs:

  1. Initial look at self-reproducing C programs .
  2. A more advanced self-reproducing C program .
  3. Putting arbitrary code into a self-reproducing C program .


B Deconstructing a simple self-reproducing C program: For this part you are to work with link number 3 directly above. This link first gives a (perhaps surprisingly) short C program that will reproduce itself. Then the write-up analyzes (deconstructs) the program to show how it works, with emphasis on output using the printf function. At the end of the write-up is an even shorter self-reproducing program. For this recitation, you are to produce an analysis similar to my analysis in the write-up. As a hint, you should realize that C treats a char variable as if it were an integer, namely the integer giving its Ascii code.

You should first reformat the program for clarity, and then show piece-by-piece how the format string is used and what is printed out. You should also answer the question at the end of web page number 3 above.


C. Deconstructing the more advanced self-reproducing C program: For this part you should work with link number number 4 above. This gives a more sophisticated self-reproducing C program, but it is also somewhat easier to understand. One key to understanding is the representation of a character inside double quotation marks.

Character Representation
in a string
newline \n
" \"
\ \\
tab \t

The web page at link 4 tries to help by giving strings in red and escape sequences (representing one character) in yellow. (Escape sequences can only occur inside a string (inside double quotes) or as a single character (inside single quotes).)

You should deconstruct this C program, explaining how it works, in particular, explaining how it is able to reproduce itself. You should use the bold line numbers at the left (not part of the source) to refer to parts of the program. As a few hints, notice that the program starts out with the declaration and initialization of an array of strings. The most confusing part of this code for me was that I didn't notice that the two cases together at line 28 (starting with case '\\': case '\"': putchar('\\');) did not have a break at the end, so in case a \ or a " is encountered, the program will print a \ followed by a \ or a ", which uses the default case. Finally, notice that the code prints the array of strings in two different ways.


What you should submit: Refer to the submissions directions and to deadlines at the top of this page. The text file that you submit should first have Your Name, the Course Number, and the Recitation Number. The rest of the file should have the following in it, in the order below, and clearly labeled, including at the beginning the appropriate item letters: a, b, c, etc.

 Contents of email submission for Recitation 7:

Last Name, First Name; Course Number; Recitation Number.

a. Give your program cargs.c along with runs for the given test data (four runs), as described in part A above.

b. Give your deconstruction of the simple self-reproducing program as described in part B above. Also try to answer the question at the end.

c. Give your deconstruction of the more-complicated self-reproducing program as described in part C above.


Revision date: 2005-03-05. (Please use ISO 8601, the International Standard.)
Copyright © 2011, Neal R. Wagner. Permission is granted to access, download, share, and distribute, as long as this notice remains.