Useful C on-line course themes etc.:
make) and then run them;
manto find out about C library functions, and the use of linker (
-l) flags and
part2.c, that uses:
Copy the starting files into your
/opt/info/courses/COMP26120/problems/ex2 (use this path) These files are:
HelloWorld.c using the command (the > is the
command-line prompt - you don't need to type it):
> make HelloWorld
You should see a single line of output:
gcc -g -std=c99 -Wall HelloWorld.c -o HelloWorld
Now run the resulting program using the command:
(or, depending on your $PATH variable, you may need to use
Again, you should see a single line of output:
You just used the
make command, which gets information from
makefile, to compile a C program.
gcc (the Gnu C Compiler) to create a binary program
HelloWorld, which can then be run directly without the use of
an interpreter (i.e. unlike Java programs, you don't have to use e.g.
java HelloWorld to run your compiled program).
man command (e.g. type
man gcc) to find
out about the command parameters
make gave to
gcc. Write notes about what you discover in
HelloWorld.c to comment out the
line and recompile it. Write down the error-messages
you get, to help you identify this sort of problem in the future.
man command to find out about
if you type
man printf (try it!) you will get output starting:
PRINTF(1) User Commands PRINTF(1)
which tells you that this is not what you were looking for - instead, you
need to type
man 3 printf to get information about the function
in the C library:
PRINTF(3) Linux Programmer's Manual PRINTF(3)
Before you proceed, write notes about what you have done in
howto so that you will be able to cope with similar things in
and run it with the command:
java SalaryAnalysis salary-data.txt
to see the output you should expect from the C version of this program.
make as in step 1A
make SalaryAnalysis ).
You should see an error message from
"undefined reference to `lround'".
gcc has compiled the program without problems, but then
called on the linker,
ld, to add pre-compiled code from the
libraries to your compiled program, and
ld cannot find the
library code for
lround. Look at the synopsis section
The first line says what
to use (check this in the C code), and the last line says what compiler and
linker flags to use - you need to have a linker flag of
To get this, edit the
makefile to remove the
(the comment symbol) from the start of the line:
make again to recompile the program. Now run it using:
and compare the output with that from the Java version. What differences are there?
Write notes about all of this in
howto, to help you identify and
deal with these sorts of problem in the future.
part2.c, for all steps of this part. You should probably keep a working back-up each time you progress to a new step.
The steps become harder, with less hints, as you progress. If you are running out of time, don't try to complete all steps - instead get everything marked that you can by the deadline, and try to prepare better for the next lab exercise. (Ensure that you use submit to prove that you finished in time.)
Write a C program to read characters one-by-one from standard input (you
can use ctrl-D to terminate the input), convert all upper-case characters to
lower-case and all lower-case characters to upper-case, and write the result
to standard output e.g.:
You should also count how many characters you have read, and how many of those you have converted in each direction, and output the totals at the end e.g.:
Read 13 characters in total, 8 converted to upper-case, 2 to lower-case
You will need to use getchar and putchar for the individual characters, as well as printf for the final character counts.
You may want to use some of the functions in
ctype.h, such as
Edit your program so that the input is read from a file
opened from within your program. For the time being, you can use a fixed
file-name such as
Hint: Stream Manipulation
Remember to check that the file is correctly opened.
Test this by running the program when there is no input file available.
Hint: refer to
man fopen and
Edit your program so that the output is written to a file,
using a fixed file-name such as
Remember to check that the file is correctly opened. Test this by running
the program when a file of that name exists, but is write protected
chmod u-w output
Edit your program so that, instead of using fixed filenames such as "input" and "output", both file-names are read from standard input when the program is run (i.e. use scanf or similar; I don't want you to use command-line parameters).
submitas normal. They will look for
2 marks - sensible notes in
(including using gcc, make, man, ld etc., and using C libraries)
2 marks - Step 2A: upper-case and lower-case, and counts
2 marks - Step 2B: input from a file
2 marks - Step 2C: output to a file
2 marks - Step 2D: reading the file-names from standard input