Shell Shell

Teach Yourself C Programming in 21 Days

Examples from the book by P. Aitken and B.L. Jones

Download all examples in a zip file

All programs copyright P. Aitken and B.L. Jones.


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:

Note: the following was generated automatically by a C program. This program works its way through the directory, looking for *.c files, sorts them alphabetically by filename, and extracts any leading comments. It then generates the appropriate HTML. Just one practical use for C!

calc.c
/* Module containing calculation functions. */
ex2-2.c
/* EX2-2.C */
ex2-4.c
/* EX2-4.C */
ex2-5.c
/* EX2-5.C */
expenses.c
/* EXPENSES.C--Demonstrates use of an array */
grades.c
/*GRADES.C--Example program with array */
/* Get 10 grades and then average them */
hello.c
 
list10_1.c
/* Demonstrates the numeric nature of char variables */
list10_2.c
/* Demonstrates printing extended ASCII characters */
list10_3.c
/* Demonstrates the use of malloc() to allocate storage */
/* space for string data. */
list10_4.c
/* Demonstrates displaying strings with puts(). */
list10_5.c
/* Demonstrates using the gets() library function. */
list10_6.c
/* Demonstrates using the gets() return value. */
list10_7.c
/* Demonstrates using scanf() to input numeric and text data. */
list11_1.c
/* Demonstrates structures that contain other structures. */
/* Receives input for corner coordinates of a rectangle and
calculates the area. Assumes that the y coordinate of the
upper-left corner is greater than the y coordinate of the
lower-right corner, that the x coordinate of the lower-
right corner is greater than the x coordinate of the upper-
left corner, and that all coordinates are positive. */
list11_2.c
/* Demonstrates a structure that has array members. */
list11_3.c
/* Demonstrates using arrays of structures. */
list11_4.c
/* Demonstrates stepping through an array of structures */
/* using pointer notation. */
list11_5.c
/* Demonstrates passing a structure to a function. */
list11_6.c
/* Example of using more than one union member at a time */
list12_1.c
/* Illustrates variable scope. */
list12_2.c
/* Illustrates variable scope. */
list12_3.c
/* Illustrates declaring external variables. */
list12_4.c
/* Demonstrates automatic and static local variables. */
list12_5.c
/* Demonstrates local variables within blocks. */
list1310.c
/* Demonstrates the system() function. */
list13_1.c
/* Demonstrates the break statement. */
list13_2.c
/* Demonstrates the continue statement. */
list13_3.c
/* Demonstrates the goto statement */
list13_4.c
/* Demonstrates using an infinite loop to implement */
/* a menu system. */
list13_5.c
/* Demonstrates the switch statement. */
list13_6.c
/* Demonstrates the switch statement correctly. */
list13_7.c
/* Demonstrates using an infinite loop and the switch */
/* statement to implement a menu system. */
list13_8.c
/* Another use of the switch statement. */
list13_9.c
/* Demonstrates the exit() and atexit() functions. */
list1410.c
/* Demonstrates some uses of scanf(). */
list1411.c
/* Demonstrates putchar(). */
list1412.c
/* Using putchar() to display strings. */
list1413.c
/* Demonstrates puts(). */
list1414.c
/* Demonstration of printf(). */
list1415.c
/* Can be used to demonstrate redirection of stdin and stdout. */
list1416.c
/* Demonstrates printer output. */
list14_1.c
/* Demonstrates the equivalence of stream input and output. */
list14_2.c
/* Demonstrates the getchar() function. */
list14_3.c
/* Using getchar() to input strings. */
list14_4.c
/* Demonstrates the getch() function. */
list14_5.c
/* Using getch() to input strings. */
list14_6.c
/* Demonstrates reading extended keys from the keyboard. */
list14_7.c
/* Demonstrates a function key driven menu. */
list14_8.c
/* Demonstrates the fgets() function. */
list14_9.c
/* Clearing stdin of extra characters. */
list1510.c
/* Passing a pointer to a function as an argument. */
list1511.c
/* Inputs a list of strings from the keyboard, sorts them */
/* in ascending or descending order, then displays them */
/* on the screen. */
list15_1.c
/* Demonstrates pointers and multidimensional arrays. */
list15_2.c
/* Demonstrates the sizes of multidimensional array elements. */
list15_3.c
/* Demonstrates pointer arithmetic with pointers */
/* to multidimensional arrays. */
list15_4.c
/* Demonstrates passing a pointer to a multidimensional */
/* array to a function. */
list15_5.c
/* Initializing an array of pointers to type char. */
list15_6.c
/* Passing an array of pointers to a function. */
list15_7.c
/* Inputs a list of strings from the keyboard, sorts them, */
/* then displays them on the screen. */
list15_8.c
/* Demonstration of declaring and using a pointer to a function. */
list15_9.c
/* Using a pointer to call different functions. */
list1610.c
/* Copying a file. */
list1611.c
/* Demonstration of temporary filenames. */
list16_1.c
/* Demonstrates the fopen() function. */
list16_2.c
/* Demonstrates the fprintf() function. */
list16_3.c
/* Reading formatted file data with fscanf(). */
list16_4.c
/* Direct file I/O with fwrite() and fread(). */
list16_5.c
/* Demonstrates ftell() and rewind(). */
list16_6.c
/* Random access with fseek(). */
list16_7.c
/* Detecting end-of-file. */
list16_8.c
/* Demonstrates the remove() function. */
list16_9.c
/* Using rename() to change a filename. */
list1710.c
/* Searching with strcspn(). */
list1711.c
/* Searching with strspn(). */
list1712.c
/* Searching with strstr(). */
list1713.c
/* The character conversion functions strlwr() and strupr(). */
list1714.c
/* strrev(), strset(), and strnset(). */
list1715.c
/* Demonstration of atof(). */
list1716.c
/* Using character test macros to create an integer */
/* input function. */
list17_1.c
/* Using the strlen() function. */
list17_2.c
/* Demonstrates strcpy(). */
list17_3.c
/* Using the strncpy() function. */
list17_4.c
/* The strdup() function. */
list17_5.c
/* The strcat() function. */
list17_6.c
/* The strncat() function. */
list17_7.c
/* The strcmp() function. */
list17_8.c
/* The strncmp() function. */
list17_9.c
/* Searching for a single character with strchr(). */
list18_1.c
/* Passing arguments by value and by reference.
list18_2.c
/* Using type void pointers. */
list18_3.c
/* Functions with a variable argument list. */
list18_4.c
/* Function that returns a pointer. */
list19_1.c
/* Demonstrate some math functions */
list19_2.c
/* Demonstrates the time functions. */
list19_3.c
/* The assert() macro. */
list19_4.c
/* Demonstration of error handling with perror() and errno. */
list19_5.c
/* Using qsort() and bsearch() with values.*/
list19_6.c
/* Using qsort() and bsearch() with strings. */
list20_1.c
 
list20_2.c
/* Demonstrates calloc(). */
list20_3.c
/* Using realloc() to change memory allocation. */
list20_4.c
/* Using free() to release allocated dynamic memory. */
list20_5.c
/* Accessing command-line arguments. */
list20_6.c
/* Demonstrating the shift operators. */
list21_1.c
/* Inputs a number and displays its square. */
list21_2.c
/* Module containing calculation functions. */
list21_4.c
 
list3_2.c
/* Demonstrates variables and constants */
list4_3.c
/* Demonstrates the use of if statements */
list4_4.c
/* Demonstrates the use of if statement with else clause */
list4_5.c
/* Demonstrates the evaluation of relational expressions */
list4_6.c
 
list5_1.c
/* Demonstrates a simple function */
list5_2.c
/* Illustrates the difference between arguments and parameters. */
list5_3.c
/* Demonstrates local variables. */
list5_4.c
/* Demonstrates using multiple return statements in a function. */
list5_5.c
/* Demonstrates function recursion. Calculates the */
/* factorial of a number. */
list6_1.c
/* Demonstrates a simple for statement */
list6_2.c
/* Demonstrates nesting two for statements */
list6_3.c
/* Demonstrates a simple while statement */
list6_4.c
/* Demonstrates nested while statements */
list6_5.c
/* Demonstrates a simple do...while statement */
list7_1.c
/* Demonstration of frequently used escape sequences */
list7_2.c
/* Demonstration using print() to display numerical values. */
list7_3.c
/* Demonstration on using scan() */
list8_4.c
/* Demonstrates the sizeof() operator */
list9_1.c
/* Demonstrates basic pointer use. */
list9_2.c
/* Demonstrates the relationship between addresses and */
/* elements of arrays of different data types. */
list9_3.c
/* Demonstrates using pointer arithmetic to access */
/* array elements with pointer notation. */
list9_4.c
/* Passing an array to a function. */
list9_5.c
/* Passing an array to a function. Alternative way. */
multiply.c
/* Program to calculate the product of two numbers. */
print_it.c
/* PRINT_IT.C--This program prints out a listing with line numbers! */
random.c
/* RANDOM.C--Demonstrates using a multidimensional array */
seconds.c
/* Illustrates the modulus operator. */
/* Inputs a number of seconds, and converts to hours, */
/* minutes, and seconds. */
size_of.c
/* SIZEOF.C--Program to tell the size of the C variable */
/* type in bytes */
square.c
/* Inputs a number and displays its square. */
unary.c
/* Demonstrates unary operator prefix and postfix modes */

Return to the
L1U10 Home Page


Anchor

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