To view the example select
Gray Code example. Note the use of a Makefile and RCS.
In class this is a handout which you should have on hand.
See also
postscript file of the lecture slides.
Let us now read through the Gray Code example program.
The points of reference on the slides are:
0. Opening comment
1. Include source code for standard i/o library
2. Define a constant (a macro facility)
3. function-procedure heading
......unsigned int (32-bits), int (2's complement integer)
......Note formal parameter declaration
4. statement of entry and exit states (pre- and post-conditions)
5. function return (x >> i) & 1
......Discuss at length
6. more elaborate function return
......return ( if (v&1) : x | (1 << i) ? x & ~(1 << i) );
7. procedure heading (void of function type)
......no return statement
8. printf, to output strings and to convert numbers to characters
9. main program, another procedure
......note argc and argv[] to accept command-line parameters
10 declaration of some variables local to main procedure
11 scanf, input equivalent of printf to convert characters to numbers
......note the use of &x to return a value
12 printf("\n"); to flush output line to display/file
13 note embedding of expressions
......Discuss some other time
14 introduction of a new block and local replacement variable i
15 note use of comment to explain loop
16 note XOR operator, ^
17 note ^= operator, where s ^= y is shorthand for s = s ^ y
18 d = bit_put(d, i, s)
......note d as both import parameter and being changed by the value
returned by bit_put.
To view the notes on specification style select Specification Style
For information on how to program in C you should read the first seven chapters in Kings, or the first five chapters of K&R (or the equivalent in any introductory book on the C-language).