Sample C Programs
The directory /u/cl/lib/labdemos/c has sample
programs that you can
copy
and study. To list these programs, type
ls /u/cl/lib/labdemos/c
Index of programs
The programs are listed in an order of study, from the least
to the most sophisticated.
-
hello
-
Everyone's first program Write a message to the screen
using
printf
.
-
harmonic1
-
Find the sum harmonic(n) = 1 + 1/2 + 1/3 + ... +
1/n where n = 20. Introduces the
for loop
.
-
harmonic2
-
A refinement of the previous program: it asks the user
to define n. Introduces
scanf
.
-
harmonic3
-
Another refinement: the user can compute harmonic(n)
many times. Introduces the
do-while loop
.