C Programming Exercises and Resources

Solutions to the 7 C exercises are now available in Blackboard.

The following seven exercises are the starting point for you to practice C programming.

  1. Hello World
  2. Factorial
  3. Getchar
  4. Currency converter
  5. Writing numbers to a file
  6. Guess a number
  7. Make the computer guess a number

These exercises are not assessed. After attempting these exercises, you should experiment further with C. Extend the exercises and try your own programs. Try ideas out, see what happens. For example, you could try to write C versions of Java programs you implemented in Stage 1. You could write your own programs to test your understanding of concepts introduced in lectures. If programs do not work the way you expect, read documentation and try to work out why. And, of course, ask for help. The more practice you do in C the better, both for learning C and for your programming skills in general.

You can organise your programs how you wish. One approach is to create a directory in your home folder called exercises and create a .c file for each exercise (e.g. ex1.c, ex2.c etc. or any more meaningful names you wish). The exercises refer to C library functions. You are expected to find out about these functions yourself. E.g. the shell command:

    # man getc

describes usage of the getc, getchar and related functions in the C stdio.h library. In addition, the C Library Reference Guide is an excellent, concise guide to C and its libraries. A list of C resources you may find useful is provided at the end of this page.

Even if you have experience with C programming, it is worth practicing now.

Compiling a C program

Use the following command on Minix to compile a C program consisting of a single source file (where myprogram.c is the source file containing your C program):

    # cc -Wall myprogram.c -o myprogram

where cc is the C compiler, the -Wall option tells the compiler to emit all warnings during compilation, myprogram.c is the name of your C source file in the current working directory and myprogram is the name you want to give to the program output by the compiler (in this case, also in the current working directory). If you omit "-o myprogram" the compiled program will be called a.out in the current working directory. After compilation, you can execute the program by typing:

    # ./myprogram

You can, of course, replace myprogram with any name you want to use for the program, and replace myprogram.c with the actual name of a C program you have written.

If you are using a MacOSX computer, you can use the same command in a terminal to compile a C program directly on your Mac (as opposed to the Minix guest VM).

If you are compiling on Linux (as opposed to the Minix guest VM), use the following:

    # cc -Wall -std=c99 myprogram.c -o myprogram

where the -std=c99 option tells the compiler you are using C99.

Exercise 1 Hello world

Write a program that prints Hello World. See printf.

Exercise 2 Factorial

Write a program that calculates the factorial of a positive integer. You should implement the following two functions:

    int rfactorial(int n);
    int factorial(int n);

rfactorial should use recursion to calculate the factorial of n and return the value of the factorial (this is equivalent to the Java method demonstrated in Part 1 of the module). The factorial function should use iteration. In your main method you can use the atoi function from stdlib.h to convert an argument to your program to an int (the number you want to calculate the factorial of).

Your functions should return an appropriate status if the factorial of a negative number is requested.

See if you can find out the maximum factorial that can be calculated before int overflow occurs.

Exercise 3 Getchar

Write a program that uses getchar to read 10 characters from stdin and print each character, followed by a new line.

Amend your program so that it skips a new line entered on stdin (e.g. does not echo the new line character and does not count it as one of the 10 characters).

Amend your program so that it skips a space entered on stdin.

Note: CTRL-D is the EOF character on stdin.

Exercise 4 Currency converter

Write a program that converts from £s sterling to EUROS using an exchange rate entered at the command line. Your program should output a table of the following form:

UK Pounds  EUROS
     1.00   1.17
    10.00  11.70
    20.00  23.40
    30.00  35.10
    40.00  46.80
    50.00  58.50
    60.00  70.20
    70.00  81.90
    80.00  93.60
    90.00 105.30
   100.00 117.00

That is, the conversion of £1 to £100, with an exchange rate of £1 = 1.17 EUROS. You should use printf formatting to output a currency exchange table as shown above.

You will need to use a function similar to atoi to convert the command-line argument to a float exchange rate.

Exercise 5 Writing numbers to a file

Write a program that reads a positive integer from user input using scanf and then prints the integers from 1 to the number given as user input to an output file. Your program should include appropriate error checking and, for example, should not create an output file if the user input is less than 1.

scanf takes a format string of the same form as printf and scans user input for matches to that format string, storing matches on pointers provided as parameters to the function.

You will also need to use file operations: fopen, fprintf and fclose.

Do your own experiments to read from and write to a file (including appending to a file). It is important to always close a file after use. It is also important that you check the status of file operations

Exercise 6 Guess a number

Write a program that picks a random number between 1 and 100 inclusive. It then prompts the user to guess the number. If the user guesses correctly, print out a message of congratulations and exit. Otherwise, tell the user whether their guess was low or high and ask them to guess again. Repeat the process. If the user takes more than 7 guesses, tell them they need to think harder and exit. You should use library functions srand and rand to seed and generate a random number. The time function provides a good starting point for srand.

Exercise 7 Make the computer guess a number

Turn the problem in Exercise 6 around. Ask the user to select a number. Then the program (computer) should guess and ask the user whether it's guess is correct, higher or lower. The user enters an appropriate response. It is up to you to decide how the user indicates correct, higher or lower (hint: string comparison is best avoided). Your program should implement a sensible guessing strategy. Try to implement the optimal strategy.

C programming resources

Back to CSC2025 resources index ...