1. Goals for this week:
-
Learn how to write C library code, and how to link it into programs that use it
-
Learn what .c and .h files are
-
More practice with strings in C (and using the readline library)
2. Starting Point Code
Start by creating a week11 directory in
your cs31/weeklylab
subdirectory and copying over some files:
cd ~/cs31/weeklylab
mkdir week11
cd week11
pwd
/home/you/cs31/weeklylab/week11
cp ~newhall/public/cs31/week11/* .
ls
3. Writing a C library
We are going to learn how to write a C library and how to use it in a program.
First, let’s look at some documentation about Libraries in C. We are going to look at the "CREATING AND USING YOUR OWN LIBRARY CODE" part of this. This content is also available in in Section 2.9.6 of the textbook.
Next, lets look at an example C library implementation, starting with the
library interface in mylib.h
, and the implementation in mylib.c
.
Then let’s look at an example program that uses it (prog.c
), and
try compiling and running it.
4. make and Makefiles
We have been using make
and Makefiles
all semester. When make
is run
it reads the Makefile
to find the rules for compiling or for cleaning up
compiled files.
We are not going to talk about make today, but if you are curious,
here is some documentation about writing makefiles:
Makefiles.
After looking this over, open in vim the Makefile
you copied over with
today’s in-class code and see if you understand what it is doing. I’ve added
some comments to describe some parts of it. Also, fancyMakefile
is another
version of a Makefile for building the prog
executable. It uses some more
advanced features of make. You could try compiling using it by
running make -f fancyMakefile
, and make clean -f fancyMakefile
.
5. Strings and chars in C
Let’s look at the file str.c
. This code contains some examples of
manipulating strings (and individual chars in a string) in C.
Remember, that in C, a string is an array of char with a special
terminating null character '\0'
that signifies the end of the string.
The array
of chars can be statically or dynamically allocated (by calling malloc).
One thing to remember is to allocate enough space for the terminating
null character.
Let’s take a look at this code and see what it is doing. Note its uses
the ctype and string library functions. C string library functions assume
that the caller has allocated space for the result string (note the call
to strcpy
).
Some example use of string library functions:
strcmp, strlen, strcpy, strchr,
Some example use of C library functions for testing char values:
isalnum, isdigit, isspace
This code also shows an example of using the readline library to read in a string entered by the user. There is some documentation about the readline library here: The readline library. The call to readline returns a string (allocated in heap space) to the caller containing the contents of the input line. It is the caller’s responsibility to free this returned string.
5.1. Try out
-
In another window, compile and run this program to see what it is doing. Note when it is manipulating the individual char in the string (array of chars) and when it is treating it as a string (using strcpy, strlen).
Try running with some different input strings, for example:
hello 1 2 3
hello 1 2 3
!@ hello x%
-
Next, try implementing code to create a substring for the first token in the str string: ---
-
search for the start of the first token (the first non-space char)
-
continue processing chars until find end of first token (the first space char following sequence of non-spaces)
-
change the char at the end of the first token to '\0' ---
Make the substr pointer point to the beginning of the first token and try printing it out. One way to set a pointer to point to any bucket in an array is:
ptr = &(array[i]);
Compile and try running with different input strings and see if it works.
-
6. Handy References
-
Textbook. Chapter 2: C programming (strings, arrays), and (2.9.6 writing C libraries) Chapter 3: C debugging tools (valgrind, gdb for C).
-
man pages for ctype and string library functions to learn more about how to use them.
-
My C Programming Resources and Links including the C Style Guide, I/O in C, cmdline args, arrays, libraries, Makefiles.