create new tag
view all tags

Exercises 2: C Programming


We will program most of our sensor readout programs in the C programming language and programming a few simple C programs will help us with future exercises. As soon as programs become a bit more complex it becomes important to describe how the program must be built. This description is usually done in Makefiles in such a way that the simple command make will reconstruct the entire program compiling just the code that has been modified and linking the right files and libraries needed be the program.

Exercise 2.1:

Since most of you could not finish your exercise last week, we will revisit the the calculations with octal and hex number systems. You will need this knowledge when we will work with chip registers.

What is the octal value of 101 001 110 ?

How much is 7+5 when calculated in octal?

Convert the octal value of 345 to decimal

Write down the binary representation of the hex value 0x83ab

How much is 7+5 in hexadecimal?

What is the decimal value of 0xa3?

Do these calculations and note down the results. I will pass by and check!

Exercise 2.2: The code examples from the lectures

Create subdirectories exercises_2 in ~/exercises/problems, ~/exercises/solutions, ~/exercises/doc.
You will do this for each new exercise session, so I will not tell you any more!

Download the code examples from the programming exercise page and put the file into ~/exercises/solutions/exercise_2 (create this directory!). They are attached as an archive file (file extension: .tar.gz) to this page (see on the bottom of the page). cd to ~/exercises/solutions/exercise_2 and extract the individual files with tar xvf https://afnog.iotworkshop.africa/pub/UCC_Course_2018/ProgrammingExercise/Exercise2Examples.tar.xz
Check in the man page for tar what this does.

  • Have a look at these examples, compile them and test them. Make sure you understand every little detail. If you don't: ask!

  • Type at least a few of them yourself using the text editor.

  • The compile command is gcc -o hello hello.c

  • if the source code is in a file called hello.c The binary output file will be called hello and you can execute it with ./hello.

Exercise 2.2: Hello World!

The first program to be written in any language including C is the traditionally the “Hello World!” program.

We don’t want to change this tradition and you are therefore asked to write the program that prints “Hello World!”.

Create a directory hello in ~/exercises/solutions/exercise_22 and create the source file hello.c in there.

Compile it with the gcc command and run the program to test it (./hello).

What does the dot do?

Exercise 2.3: Pointers

Create a C program(exercise23.c) with the following instructions:

read a file exercise.txt (download from the exercise page)

Use pointers to print data on screen.

Exercise 2.4: A calculator

We want to produce a calculator program capable of calculating the 4 basic calculations:

  • addition

  • subtraction

  • multiplication

  • division

The program takes 3 arguments: the first operand followed by the operator followed by the second operand. The user should type calculate 3.4 + 5.8 and the program should print:

3.4 + 5.8 = 9.2

Addition, subtraction, multiplication and division should be performed by functions implemented in separate files, compiled separately and linked to the main calculator program.

Write the 8 C files:

  • calculator.c

  • add.c

  • subtr.c

  • mult.c

  • divide.c

When trying the program you will experience a problem with multiplication. Can you figure out why? Instead of using “*” you must use “\*” and it will work.

Here we will compile the programs separately:

gcc -c calculator.c which will produce an object file calculator.o. Do the same thing with the other source files.

Finally link everything to produce the executable:

gcc -o calculator calculator.o add.o subtr.o mult.o divide.o

Exercise 2.5: write a function for binary

Create a function for converting decimal to binary.

Once your calculator works: Convert it to a calculator that calculates in decimal, octal and hex, binary?

Have a look into the man page of sscanf and printf and check for input and output conversions.

-- Isaac Armah-Mensah - 2018-09-19


Topic attachments
I Attachment History Action Size Date Who Comment
Unknown file formatxz Exercise2Examples.tar.xz r1 manage 17.1 K 2018-09-20 - 10:30 IsaacArmahMensah  
Texttxt exercise.txt r1 manage 0.1 K 2018-09-20 - 03:39 IsaacArmahMensah  
Unknown file formatodt exercise2.odt r1 manage 21.4 K 2018-09-20 - 03:24 IsaacArmahMensah  
Edit | Attach | Watch | Print version | History: r5 < r4 < r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r5 - 2018-09-27 - UliRaich
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2021 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback