Link back to syllabus

Numbers & Bits

  1. Write C code to figure out how many bytes are taken up by the following types on your machine:

    1. int
    2. short
    3. long
    4. char
    5. float
    6. double
  2. How do you write the largest int possible?

  3. Is char signed or unsigned by default on your machine?

  4. Write a program that exhibits odd behavior caused by mixing signed and unsigned integers. For example, write a program where x has value -10, y has value 10, but x < y does not hold. Why does this behavior happen?

  5. You can cast an expression of one type to another like this: (type)expr. That is, (float)5 casts the int 5 to be a float. Find 3 examples where casting a value to a type and back again results in a different value. For example, computing (int)(float)5 casts from int to float and back to int. This gives us 5 back again, but your examples should end up with a different value than you started with.

  6. Write a program using floats that demonstrates a rounding error, where the result of a computation is not what a mathematician would expect. (Hint: Because fifths are easily representable in decimal but difficult in the binary format used by a computer, working with numbers ending in .2 and .4, etc., is a good way to approach this.)

  7. Write a program to demonstrate whether your machine performs sign extension. That is, if you bit-shift a negative number to the right, does the sign bit get moved? Or does it get duplicated?

  8. Write a program that asks the user for a number and prints it out in decimal, octal, hexadecimal, and binary. For the binary case, you may find it easier to print out the value backwards – that is, with the 1’s bit first and then other bits in order of increasing value. Note that printf does not have a formatting code for binary output; you’ll have to do the heavy lifting yourself.

At this point, call me over and show me what you’ve done.

Reading the man

Answer the following questions by piping together operations on the command line.

  1. What line numbers of the manual page for wc include the word “word”?

  2. The file /usr/share/dict/american-english (on the lab computers and powerpuff) has a list of all English words. How many words are there in this file?

  3. How many files are in /usr/lib?

  4. How many files/directories in /usr/lib begin with the letters lib? Make sure not to look in subdirectories of /usr/lib. (You will need to look at the ls manpage.)

  5. How many directories are there in /usr/lib?

  6. What line deletes all files in the current directory that end in two numbers followed by .c?

  7. How can you repeat the last command you entered?

Makefiles

  1. Read this introduction to Makefiles: http://www.cprogramming.com/tutorial/makefiles.html

  2. Write a Makefile suitable to compile two files, main.c and func.c into an application app. Test your Makefile by writing a short program with a function call (it can do anything you like – perhaps just add two numbers) spread into these files.

Unit testing

  1. In reverse.c, write a function int reverse_digits(int n) that reverses the order of digits in a number.

  2. Write a corresponding reverse.h that declares the reverse_digits function.

  3. Write a check_reverse.c file that runs unit tests on the reverse_digits function.

  4. Compile and check your work.

When you’re all done, post your work in a folder you create in our lab02 folder on GitHub.