The second part introduced the so called function wraps and stubs.
By mistake, the prefix lab7 was used for program names. It should have been lab9. The links below use the correct number 9.
The first program lab9a.c is just a demonstration of an infinite while-loop with a nonzero numeric constant as loop condition. Replacing while(5) to while(0) makes the program never enter the loop instead of continuing indefinitely.
A possibility to use a number as loop condition is exploited in countdown version lab9e.c. Here the loop header is short and elegant while(i).
lab9b.c: Some for-loops.
The final version is a puzzle. The initial value of the
loop counter i is 0. Every time in the loop i
gets incremented and the condition is i>=0. And
yet the loop terminates!
(For a hint see end of comments to
Lab 4.)
The effect cannot be observed, if printf is kept
in the loop body: reaching a nonpositive value of i
takes too much time. [For those philosophically inclined:
This is an example of "quantum effect": measurment
(printing a message from inside the loop) interferes
with effect we want to observe (finiteness of the loop).]
lab9f.c: Finiteness of a mathematically infinite loop is demonstrated with a decremental while-loop.
Nested loops:
lab9c.c (with incremental counting),
lab9g.c (with decremental counting).
The programs print current values of both counters and
the total number of runs of the fastest (internal) loop.
Part II
Stub and wrap functions
are a sort of complementary to each other:
the former have the same interface as the intended
underlying (or targeted) functions, but different
functionality. The latter have the same basic
functionality but alter the interface.
In this example,
the final goal is (supposedly) to implement some mathematical
algorithm for computing mysin(x), but this is complicated
(unless using <math.h>'s function is acceptable in the
project). The programmer may first to organize all other
things in the program using a stub for mysin. It would
provide much more comfort for subsequent (hypothetical)
hard math.
Here is an example.