CS31 Weekly Lab: Week 11

writing C libraries: .c and .h files and linking, Makefiles, C strings

Week 11 lab topics:

  1. writing C library code, and linking it into programs that use it
  2. .c and .h files
  3. strings in C (and using the readline library)

Create a week11 subdirectory in your weeklylab subdirectory and copy over some files:
    cd cs31/weeklylab		
    pwd
    mkdir week11
    ls
    cd week11
    pwd
    cp ~newhall/public/cs31/week11/* .
    ls

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 and then look at some example C library code in mylib.[ch] and an example program that uses it (prog.c).

make and Makefiles
Let's take a look at Makefile and make.

Here is some documentation about writing makefiles: Makefiles

Let's open the Makefile you copied over with today's in-class code and see what it is doing

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.

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).

example string library functions: 
  strcmp, strlen, strcpy, strchr, 
example ctype library functions:
  isalnum, isdigit, isspace 

This code also shows an example of using the readline library to read in a string entered by the user. I have some documentation about the readline library here: using the C 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.

See aslo the char and string in C documentation from the "C and Part 1 of intro to C for CS31 students" webpage

Also, make use of man pages for ctype and string library functions to learn more about how to use them.

Try out

  1. 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%
    

  2. Next, try implementing your own strlen function (mystrlen). Then replace the call to strlen (at (0)) with a call to mystrlen. Your version needs to calculate the length of the passed string by processing the array of char values (i.e. you cannot just call strlen inside your mystrlen function).
  3. Next, try implementing your own strcpy function (mystrcpy). Then replace the call to strcpy (at (2)) with a call to mystrcpy.
  4. Next, try implementing code to create a substring for the first token in the str string:
    1. search for the start of the first token (the first non-space char)
    2. continue processing chars until find end of first token (the first space char following sequence of non-spaces)
    3. change the char at the end of the first token to '\0'

    Make the substr pointer point to the begining of the first token and try prining 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.