Shell Shell

Examples from the Lectures

Seahorse All the example C programs from the lecture course are here in electronic form, for your education and edification.
All programs copyright © 1996 D.C. Hamill, University of Surrey.

Download all the example programs

In a single zip file

In a gzipped tar archieve


Lecture Index

Lecture 1: Introduction Lecture 10: Basics of pointers
Lecture 2: Binary representation Lecture 11: Strings
Lecture 3: Hardware and software Lecture 12: Basics of functions
Lecture 4: Simple data types Lecture 13: More about functions
Lecture 5: Standard input and output Lecture 14: Files
Lecture 6: Operators, expressions and statements Lecture 15: Data structures
Lecture 7: Making decisions Lecture 16: Case study: lottery number generator
Lecture 8: Looping Lecture 17 (revision)
Lecture 9: Arrays Lecture 18 (revision)

The programs are listed in alphabetical order within each lecture.

To run a program:

  1. Click on the link to load the source code into the browser.
  2. Study the code to see what the program is meant to do.
  3. Select and copy all the text.
  4. Open an editor window and paste the text into it.
  5. Save the file under an appropriate name (e.g. myfile.c)
  6. Compile and run the program in the normal way:

Lecture 1: Introduction

 
intro.c
 
/* Example: C program to find area of a circle */
 
 

IndexBack to the Lecture Index


Lecture 2: Binary Representation

No program examples in this lecture.

IndexBack to the Lecture Index


Lecture 3: Hardware and Software

No program examples in this lecture.

IndexBack to the Lecture Index


Lecture 4: Simple Data Types

anatomy.c
/* Example: anatomy of a simple C program. */
define.c
/* Example: constant definition using #define */
overflow.c
/* Example: demonstrates overflow of integer variables */
/* Don't worry about how the program works, just appreciate what happens! */
sizes.c
/* Example: compile and run this program to find out how your C compiler treats different data types. The result will depend on the computer and compiler you use, and possibly on the compiler settings. */

IndexBack to the Lecture Index


Lecture 5: Standard Input and Output

getchar.c
/* Example: getting a single character from the keyboard, using getchar */
hello.c
/* Hello world program */
printf1.c
/* Example: outputting numeric data using printf */
printf2.c
/* Example: formatting integer data using printf */
printf3.c
/* Example: formatting float/double data using printf */
putchar.c
/* Example: outputting characters using putchar */
puts.c
/* Example: outputting strings using puts */
scanf.c
/* Example: inputting numeric data using scanf */

Entering Bug Zone!

printf.bug
/* BUG ZONE!!!
Example: outputting numeric data using printf */
scanf.bug
/* BUG ZONE!!!
Example: inputting numeric data using scanf */
stdio.bug
/* BUG ZONE!!!
Example: standard output */

Leaving Bug Zone

IndexBack to the Lecture Index


Lecture 6: Operators, Expressions and Statements

floatari.c
/* Example: floating point arithmetic */
intarith.c
/* Example: integer arithmetic operators */
/* Old Imperial measures: miles, furlongs, chains,
yards, feet and inches.
1 mi = 8 fur; 1 fur = 10 ch; 1 ch = 22 yd;
1 yd = 3 ft; 1 ft = 12 in */
prepost.c
/* Example: prefix and postfix ++ and -- operators */
relation.c
/* Example: relational and logical operators */
typecast.c
/* Example: type casting */

Entering Bug Zone!

intarit1.bug
/* BUG ZONE!!!
Example: integer arithmetic */
intarit2.bug
/* BUG ZONE!!!
Example: integer arithmetic */

Leaving Bug Zone

IndexBack to the Lecture Index


Lecture 7: Making Decisions

conditio.c
/* Example: conditional expressions */
if.c
/* Example: simple if statement */
ifelse.c
/* Example: if...else statement */
leapyear.c
/* Example: determining leap years */
logical.c
/* Example: logical expressions */
nestedif.c
/* Example: nested if...else statements */
switch.c
/* Example: switch statement */
switch2.c
/* Example: switch statement without breaks */
trufalse.c
/* Example: to show logical (true/false) values */
/* In C, zero means false, non-zero means true */

Entering Bug Zone!

if1.bug
/* BUG ZONE!!!
Example: if...else statement */
if2.bug
/* BUG ZONE!!!
Example: if statement */

Leaving Bug Zone

IndexBack to the Lecture Index


Lecture 8: Looping

bottles.c
/* Example: for loop exercise */
/* This program contains some sneaky (but ANSI legal) C code.
It is certainly not a model of transparent programming!
Compile and run it to see what it does. Then write your own
program to do the same, making it easily comprehensible! */
break.c
/* Example: exiting loops using break */
continue.c
/* Example: skipping to end of loops using continue */
dowhile.c
/* Example: do-while loop */
for.c
/* Example: for loops (also a simple macro) */
forcomma.c
/* Example: comma operator in for loops */
infinite.c
/* Example: 7 infinite loops with exit conditions */
/* All these loops do the same thing. */
/* Choose whichever you think fits your problem best. */
mcducks.c
/* Example: menu system, using do...while, getchar and switch */
/* You've reached a point in the course where you know enough C
to start writing real programs. Here's a fairly realistic example:
a menu ordering system. It could be improved -- for instance there's
no way to cancel an item. Also, when we get on to functions, the
structure could be made more modular. Nevertheless, it works! */
nestfor.c
/* Example: nested for loops */
while.c
/* Example: while loop */

Entering Bug Zone!

for1.bug
/* BUG ZONE!!!
Example: for loop */
for2.bug
/* BUG ZONE!!!
Example: for loop */
for3.bug
/* BUG ZONE!!!
Example: for loop */
while1.bug
/* BUG ZONE!!!
Example: while loop */
while2.bug
/* BUG ZONE!!!
Example: while loop */

Leaving Bug Zone

IndexBack to the Lecture Index


Lecture 9: Arrays

analyse.c
/* Example: analysis of text */
array1.c
/* Example: arrays */
array2.c
/* Example: arrays */
array3.c
/* Example: vectors represented by arrays */
array4.c
/* Example: matrices represented by 2-dimensional arrays */
bubble.c
/* Example: bubble sort values in array */
/* The bubble sort works by comparing values in adjacent
array positions. If the value in the "lower" position is
greater than that in the "higher" position, the two are
swapped. Thus the largest value "bubbles up" to the "top"
of the array. When there are no more values to be swapped,
sorting is complete. */
multidim.c
/* Example: multidimensional array */
/* The University of Symmetry has 3 faculties. Each faculty has
7 departments. Each department runs 5 courses. Each course takes
4 years. Each year consists of 20 students. Each student has
a University Registration Number (URN). */

Entering Bug Zone!

array.bug
/* BUG ZONE!!!
Example: arrays */

Leaving Bug Zone

IndexBack to the Lecture Index


Lecture 10: Basics of Pointers

pointer1.c
/* Example: basics of pointers, & and * operators */
pointer2.c
/* Example: pointers and arrays */
pointer3.c
/* Example: pointers and two-dimensional arrays */
scanfptr.c
/* Example: scanf and pointers */

Entering Bug Zone!

pointer.bug
/* BUG ZONE!!!
Example: some common pointer errors */

Leaving Bug Zone

IndexBack to the Lecture Index


Lecture 11: Strings

analyse2.c
/* Example: analysis of text using <ctype.h> library */
/* (Based on analyse.c) */
bubble2.c
/* Example: bubble sort strings in array */
/* Based on bubble.c. Uses strcmp to compare
strings and strcpy to swap them */
sprintf.c
/* Example: formatted printing to a string using sprintf */
/* Like printf, but prints to a string instead of standard output */
sscanf.c
/* Example: formatted input from a string using sscanf */
/* Like scanf, but scans a string instead of standard input */
strings1.c
/* Example: strings as null-terminated arrays of char */
strings2.c
/* Example: inputting strings from keyboard with scanf */
strings3.c
/* Example: inputting strings from keyboard with scanf */
strings4.c
/* Example: inputting strings from keyboard with gets */
strings5.c
/* Example: string variables as pointers to char */
strings6.c
/* Example: string manipulation using <string.h> library */
/* At the Unix prompt, enter 'man string' for details */

Entering Bug Zone!

strings.bug
/* BUG ZONE!!!
Example: some common string errors */

Leaving Bug Zone

IndexBack to the Lecture Index


Lecture 12: Basics of Functions

bisect.c
/* Example: solving f(x) = 0 by the bisection method */
/* Given a nonlinear function f(x), a value of x is sought such that
f(x) = 0. At each iteration the interval containing the root is
halved. A root in the interval is indicated by a change in the sign
of f(x). */
funct1.c
/* Example: simple functions */
/* Functions taking one or more arguments and returning a value */
funct2.c
/* Example: simple functions */
/* Function taking no arguments but returning a value */
funct3.c
/* Example: simple functions */
/* Function taking an argument but returning no value */
funct4.c
/* Example: simple functions */
/* Function taking no arguments and returning no value */
funct5.c
/* Example: simple functions */
/* Function with multiple return statements */
funct6.c
/* Example: calling a function -- why doesn't this work? */
math.c
/* Example: mathematical functions in the <math.h> library */
/* At the Unix prompt, enter 'man math' for details */
xmascard.c
/* Example: draws a Christmas card */
/* Function calling another function */

IndexBack to the Lecture Index


Lecture 13: More About Functions

array5.c
/* Example: passing arrays to functions */
array6.c
/* Example: passing 2-D arrays to functions */
exit.c
/* Example: aborting a program in an emergency, using exit */
global.c
/* Example: global variables */
/* Like goto statements, global (external) variables may seem
convenient, but experience shows they can cause many bugs. */
local.c
/* Example: local variable within a block */
main.c
/* Example: main as a function */
/* Shows use of argc and argv[] and return value */
/* You can feed arguments from the operating system shell to main.
At the Unix prompt, try entering: a.out 3 blind mice */
pointret.c
/* Example: pointer as return value of functions */
/* Often used with arrays and strings */
scope.c
/* Example: scope of variables */
static.c
/* Example: static local variables within a function */
swap.c
/* Example: function call by value and call by reference */

IndexBack to the Lecture Index


Lecture 14: Files

copy1.c
/* Example: copying a file */
/* Like Unix's cp command. When compiled, rename a.out to copy,
then use as: copy filename newname */
/* Warning: THIS SKELETON VERSION CONTAINS NO BOMB-PROOFING! */
copy2.c
/* Example: copying a file */
/* Like Unix's cp command. When compiled, rename a.out to copy,
then use as: copy filename newname */
/* This version has pretty good bomb-proofing */
files1.c
/* Example: simple file operations */
/* No error checking is done, so the program may fail!
All practical programs should check file operations. */
files2.c
/* Example: standard file operations, with error checking */
/* All practical programs should check file operations. */
marks1.c
/* Example: writing data to a file */
marks2.c
/* Example: reading data from a file */
/* We know the number of lines in the file */
marks3.c
/* Example: reading data from a file */
/* The number of lines in the file is unknown */

IndexBack to the Lecture Index


Lecture 15: Data Structures

complex1.c
/* Example: using structures to represent complex numbers */
complex2.c
/* Example: structures as function arguments and return values */
complex3.c
/* Example: pointers to structures */
stores.c
/* Example: array of structures -- stores items */
students.c
/* Example: building a complicated data structure, step by step */
typedef.c
/* Example: using typedef to define a structure type */
/* This program is based closely on complex2.c; compare them */

IndexBack to the Lecture Index


Lecture 16: Case Study: Lottery Number Generator

lottery1.c
/* Example: National Lottery numbers selector */
/* Version 1: Comprises main and two function stubs. */
lottery2.c
/* Example: National Lottery numbers selector */
/* Version 2: Global array made local to main, and passed
as a function argument. Random number generator added.
Selected numbers are now printed. */
lottery3.c
/* Example: National Lottery numbers selector */
/* Version 3: Random number generator seeding added. Integer
lottery4.c
/* Example: National Lottery numbers selector */
/* Version 4: Duplicate numbers are rejected */
lottery5.c
/* Example: National Lottery numbers selector */
/* Version 5: Numbers are now printed in order. */
lottery6.c
/* Example: National Lottery numbers selector */
/* Final version, tidied up and documentation improved */
lottery7.c
/* Example: National Lottery numbers selector */
/* An alternative version, based on a different data model */

IndexBack to the Lecture Index


Return to the
L1U10 Home Page

No animals were harmed in the making of this Web page. Contains no artificial colour, preservatives or additives. Produced with 100% recycled electrons. Any resemblance of these programs to any other program living or dead is unintentional and coincidental.


Anchor

David C. Hamill
D.Hamill@surrey.ac.uk