MSc Computer Systems – C Programming MSc Real Time Systems
C Programming Exercises

Prepared by Eric Goodyer

Room 5.11 Gateway
For more information mail me at eg@dmu.ac.uk

These exercises can be carried out using either the TURBO C compiler in Q1.01, or GNU C compiler in Q 3.01/2. The purpose of these exercises is to illustrate the aspects of the underlying hardware architecture of a computer, they are not intended to provide a thorough understanding of the C programming language.
To use the TURBO C compiler in Q 1.01 you must restart the PC in DOS mode. So switch off the PC, switch it on again, and during the boot up sequence select DOS mode. You will start up in the USER directory. Type TC to the DOS prompt to start up the DOS graphics interface environment.
To get a free copy of Turbo C go to Borland's web siteThen join 'Borland Community' - this allows you to download 'antique software' free of charge. You will find turbo C in the list. To obtain a free copy of the C compiler go to http://gcc.gnu.org where you will find a library of useful free software tools.

Ian Vlaiminkes C Tutorials See these too

Exercises

quick link to 'EXERCISES 2 – ADDRESSING MODES' Lab notes on addressing modes
quick link to 'EXERCISES 3 – FILES' Lab notes on files


1 Getting Started & Data Types

Before we can explore the C programming language you need a lesson in how to use the tool itself.
// list the include file
#include <direct.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <dos.h>

void main()
            {
            printf("Hello World\n\r");
            getch();
            }

We will start with the above programme (http://www.cse.dmu.ac.uk/~eg/msc_cs/cs_lab1.c) which will print a message onto your screen, and wait for you to press any key on the keyboard.

Use the menu bar command ‘file’ to open and save files. The screen is a full screen editor. Use the ‘compile’ command to compile the programme, and the ‘run’ command to run it.

Note also the Help facility – which is incredibly useful as it describes all the major C operations and provides example code that can be cut & pasted into your own programmes.

Once you have understood how to use these basic tools, move on to using C variables.

There are a number of C variable types. char int and long are signed 8-bit, 16-bit and 32-bit integers. Unsigned varieties are also available and are declared as unsigned char, unsigned int and unsigned long.

Try the following programme http://www.cse.dmu.ac.uk/~eg/msc_cs/cs_lab2.c, which also introduces the concept of a formatted print.

void main()
            {
            unsigned char a; // create a signed char variable
            unsigned int b; // create a signed int variable
            unsigned long c ; // create a signed long variable
            printf("My Test Programme\n\r");
            a = 0 ;
            while (!kbhit()) // stay in this loop until a key is pressed
                        {
                        printf("%d ",a); // print variable a as a decimal number followed by a space, %d means decimal
                        a++; // increment variable a delay(100); // wait 100ms
                        }
            getch();
            }

When you run this programme you will see the current value of the variable a displayed on the screen. It will have a numeric range from 0 to 255. When it reaches 255 it will go back to zero – WHY?

Now repeat this for the unsigned integer. http://www.cse.dmu.ac.uk/~eg/msc_cs/cs_lab3.c

void main()
            {
            unsigned char a; // create a signed char variable
            unsigned int b; // create a signed int variable
            unsigned long c ; // create a signed long variable
            b= 0 ;
            while (!kbhit()) // stay in this loop until a key is pressed
                        {
                        printf("%d ",b); // print variable b as a decimal number followed by a space, %d means decimal
                        b += 256; // add 256 to variable b
                        delay(100); // wait 100ms
                        }
            getch();
            }

Note that the variable wraps around when it is greater than 65535.

Finally repeat this for the unsigned long variant.  http://www.cse.dmu.ac.uk/~eg/msc_cs/cs_lab4.c

void main()
            {

            unsigned char a; // create a signed char variable
            unsigned int b; // create a signed int variable
            unsigned long c ; // create a signed long variable
            c= 0 ;
            while (!kbhit()) // stay in this loop until a key is pressed
                        {
                        printf("%lu ",c); // print variable c as a decimal number followed by a space, %lu means long unsigned decimal
                                c += 8192; // add 8192 to variable 2

                               delay(20); // wait 20ms
                                }
            getch();
               }

Now repeat all these experiments using signed variables and note the differences.
Now we introduce the floating point types, these are float and double.

void main()
            {
            float e ; // declare a floating point variable
            while (!kbhit())
                        {
                        printf("%f ",u);
                        u += 1000.05 ; // add 1000.05 to e
                        delay(50); // wait 50ms
                        }
            getch();
            }

This programme adds 1000.05 to e each cycle, but note that it very quickly loses resolution at the lower end. This is because a floating-point mantissa only has 24 bits, and it simply cannot cope with numbers that have a wide dynamic range. To prove that it can cope with 0.5 change the above programme to add only 0.5 to variable e.
Now replace the float declaration with a double declaration.

http://www.cse.dmu.ac.uk/~eg/msc_cs/cs_lab5.c

void main()
            {
            double e ; // declare a double precision floating-point variable
            while (!kbhit())
                        {
                        printf("%lf ",u);
                        u += 1000.05 ; // add 1000.05 to e
                        delay(50); // wait 50ms
                        }
            getch();
            }

Using double precision improves resolution, but is slower and requires more memory.
What happens if we try to mix up our data types? If you try to force an int variable into a char variable it will simply be truncated to 8-bits. You should not mix up data types, and some compilers will warn you if you try to do it.
Try the following

http://www.cse.dmu.ac.uk/~eg/msc_cs/cs_lab6.c

void main()
            {
            unsigned char a ;
            unsigned int b ;
            b = 1025 ; // load 1025 into the unsigned integer b
            a = b ; // put in into a
            printf("%d ",a); // print it out
            getch(); // wait for a key stroke
            }

When this programme is run the value of 1025 (100 00000001 in binary) is simply truncated to 8 bits.

What you have learnt today

EXERCISES 2 – ADDRESSING MODES

Lab notes on addressing modes