Laboratory Exercises For Computer Science 211

Elementary C Programming

Elementary C Programming

Goals: This laboratory is designed to:

Steps for this Lab: Since C does not contain a designated Boolean data type, integers are used. By convention, the integer value 0 represents false, and any nonzero integer value is interpreted as true.

Program ~walker/211/labs/prog1.c includes code to print part of a truth table.

  1. Copy this program to your account, and compile it with the statement:
    gcc -o prog1 prog1.c
    Then run the program by typing prog1 .

    Note: The compiling and running steps may combined with the command:

    gcc -o prog1 prog1.c && prog1
  2. Add the names of those in your group as comments at the start of the program. Then complete the truth table by defining and using the following functions:
      void nor (int a, int b, int * res)
      void and (int a, int b, int * res)
      int or (int a, int b)
    In each case, the function should perform the designated Boolean operation. Specifically, define nor directly with C operations. Then write and and or by calling any or all of not, nand, and nor.

  3. Write a program that reads two 2-bit numbers (a1a0 b1b0) and computes their sum using only the operations not, nor, nand, and and or as defined in the previous program. That is, follow the logic for a gate-level adder to perform the addition.

    To further simulate gate-level circuitry, consider each variable as the name of a separate wire in a circuit diagram. That is, follow these rules in writing your program:

    To simplify input, you may assume each 2-bit number is entered with a space between the bits, so reading of a number may be accomplished with the statement :
         scanf ("%d %d", x1, x0);

Work to be turned in:

Form for listing, compiling, and running programs:

  1. Use the submit command to record the activity in a dtterm window.
  2. Use cat filename to display the program in the dtterm window.
  3. Compile the program with the gcc command.
  4. Run the program sufficient times to show it runs correctly.
  5. After printing the above session, write (by hand) on the output a justification of why your testing demonstrates that your code is correct.

This document is available on the World Wide Web as

created September 26, 1997
last revised October 1, 1997