Summary: In this laboratory you will begin working with the C programming language.
Prerequisites: None.
Contents:
a. In the last lab, you entered the following program and compiled it using the command "gcc -Wall welcome.c" at the shell prompt. You then added a command to your Emacs initialization file that allows you to invoke the same command by selecting Compile... from the Tools menu.
#include <stdio.h>
int main() {
printf("Welcome to CSC201.\n");
return 0;
}
Recall that compiling with this command produces an executable file named a.out in your working directory. Recall also that the option -Wall asks gcc to give you all the warning information it can. Again, I urge you to use this option and to pay attention to the warnings it gives since they often indicate true bugs. For this reason, you should always endeavor to write programs with no warning messages.
b.
Now let's compile a slightly different C program. Please create a new source
file named sqrt.c containing the program below, and then try to
compile the program. (Although, you may not with our version of gcc, you
should get an error message, saying that the function sqrt
can not be found.)
#include <stdio.h>
#include <math.h>
int main() {
printf("The square root of two is %g\n", sqrt(2.0));
return 0;
}
C provides a collection of math functions in the "math library", and
sqrt is among these functions. To use them you need to do two
things: first, include the line "#include <math.h>" in your
program since the functions are declared in the C system file
math.h, and second, add the option "-lm" to the end
of your compile command to specify that your program should be "linked" with
the math library. (Note that the option contains a letter l, for library, not
a number 1.)
Build (i.e., compile and link) the program again, using the following
command. It should work this time.
gcc -Wall sqrt.c -lm
Note that the function printf is in the "C library", a
collection of C functions that is linked to by default. To use it, we need
to include the line "#include <stdio.h>", but we don't need
to add anything special to the compile command. Almost all of the
functions C provides are in the C library; math functions are the
exception. Thus, "-lm" is the only option of its type you need
to learn.
a.
Very soon, we will explore the many ways that
printf can be used to format output, but here are a few basic
formats to get us started.
You can print a string literal as follows.
printf("Enter a number: ");
To print the value stored in an integer variable, use the following
format. The value in the variable size will be substituted in
place of the "%d" in the output.
int size = 8;
printf("My shoe size is %d\n", size);
Printing doubles is similar to printing integers, but requires a different format character. The following format (among others) can be used.
double pi = 3.1415;
printf("The (rounded) value of pi is %g\n", pi);
The "\n" in the examples above is the "newline"
character. It will cause the next item printed to start at the beginning of
a new line.
b. As you know, interesting programs include boolean expressions to control program flow. Therefore, we need a data type to store the results of such expressions. However, C does not have a boolean data type.
In C, boolean expressions are of type int.
A logical expression that evaluates conceptually to false will be
stored internally as the integer 0. Expressions that evaluate
conceptually to true are stored as the integer 1.
What is the truth-value of each of the following expressions? How will that value be stored in C?
(1 + 1 == 3)
(1 + 2 == 4) || (1 + 1 == 2)
(1 + 2 == 4) || ((1 + 1 == 2) && (2 + 2 == 4))
Write a short program that prints the evaluated value of each of the expressions given above.
c.
Similarly, int variables can be used in situations that
call for boolean values. For example, we can write the following. How do
you expect this code snippet to behave (i.e., will it "keep looking")?
int found = 0;
while (!found) {
//keep looking
...
}
Moreover, if the compiler allows int variables in situations
that call for boolean values (as it does), there is nothing stopping us
from using integer values other than 0 or 1. Doing so is not common, but it
is worth knowing what happens in such a case.
Consider the code snippet below. If you were designing C, what would you
choose for the code to do in this case?
int n = 25;
while (n) {
//keep looking
...
}
Write a short program to check whether the designers of C came to the same conclusion you did.
d. Write a program that uses a loop to add the numbers 1 through 10, and then prints the result (which will be 55).
e. As you probably know, the sum of the integers from 1 to n is (n)(n+1)/2. Write a program that verifies this formula, for 1<=n<=100. That is, your program will loop over the values of n from 1 to 100, compute the sum as done in part d, and compare it to the result from the formula. For each discrepancy found, your program should output a message such as "formula doesn't work for n=35". (Of course, if your program is correct, there will be no output.)
f. Write a program that outputs the first 10 multiples of the number 3, printing each number on a separate line. In addition, print "even" or "odd" after each number, as appropriate. And don't you dare write 10 separate printf statements!