CS 246 Lab 2
Spring 2017
Write C code to figure out how many bytes are taken up by the following types on your machine:
int
short
long
char
float
double
How do you write the largest int
possible?
Is char
signed or unsigned by default on your machine?
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?
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.
Write a program using float
s 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.)
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?
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.
man
Answer the following questions by piping together operations on the command line.
What line numbers of the manual page for wc
include the word “word”?
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?
How many files are in /usr/lib
?
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.)
How many directories are there in /usr/lib
?
What line deletes all files in the current directory that end in two numbers followed by .c
?
How can you repeat the last command you entered?
Read this introduction to Makefiles: http://www.cprogramming.com/tutorial/makefiles.html
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.
In reverse.c
, write a function int reverse_digits(int n)
that reverses the order of digits in a number.
Write a corresponding reverse.h
that declares the reverse_digits
function.
Write a check_reverse.c
file that runs unit tests on the reverse_digits
function.
Compile and check your work.
When you’re all done, post your work in a folder you create in our lab02 folder on GitHub.