Comp 210 Lab 10: C

Edit, Compile, Run, Errors


Edit, Compile, Run

The process for writing and running C programs is different that for Scheme programs because we don't have an equivalent of DrScheme or Donkey. Instead our process is

  1. Write/edit your program in an editor (recommended: emacs).
  2. Compile your program using g++.
  3. Run the result of the compilation step.

As an example, type in the following small program. In Emacs, open the new file c2f.cc and type in the following code. Note that Emacs recognizes the .cc file extension and puts you in a C++ mode that helps you format your code. When you are done typing, save the file.

#include <iostream.h>

float c2f(float c)
{
  return 9*c/5+32;
}

void main()
{
  float c;
  
  cin >> c;
  cout << c2f(c) << endl;
}

Now compile the code. In your xterm, type g++ c2f.cc -o c2f. That means, "Run the program g++ (our compiler) on file c2f.cc and put the output in the file c2f. Assuming you typed in the code correctly, this takes a minute and finishing without printing anything, meanwhile creating the file c2f.

For the curious: Compiling is the process of taking your "source code", the more-or-less human-readable C/C++ code c2f.cc, and producing "object code" c2f that the computer understands. Learn more about this in Comp320 and Comp412. If you don't tell the compiler the name of the output file, it defaults to using a.out.

Now run the compiled code. In your xterm, type c2f. Now our program expects us to type in the celsius temperature to convert, so enter a number. Our program then returns another number, the equivalent fahrenheit temperature.

To convert another temperature, run the program again. You don't have to recompile the program unless you change the program.

For PC users: There are C++ compilers for PCs which give you DrScheme-like environments. Feel free to use them. gnuemacs and g++ are available via the web -- the daring can try to download and install them.


Errors

As in DrScheme, you can encounter errors at two times:

At least in principle, C/C++ can find more mistakes than Scheme during compile-time, as it looks for type-errors. However, C/C++ run-time errors are generally very uninformative. During this section of lab, we will give you a quick introduction to diagnosing and fixing some of your errors.

Copy the program /home/comp210/public_html/Labs/lab10/error1.cc into your directory and compile it. It complains

error1.cc: In function `float c2f(float)':
error1.cc:6: parse error before `}'
A number just after the file name indicates the line number in the file where this error occurs. I.e., in the function c2f, there is a syntax error just before the closing brace on line 6. Look at the file -- can you fix the problem?

Any time it complains about a "parse error", that means you've left something out that is necessary (like a comma or semicolon or keyword), put something extra in, or mistyped something (like misspelling a keyword).

Copy and compile the program /home/comp210/public_html/Labs/lab10/error2.cc. It complains

error2.cc:3: parse error before `('
error2.cc: In function `int main(...)':
error2.cc:13: warning: `float' used for argument 1 of `c2f(int)'
The second two lines of errors mean that in the function main, on line 13 we call the function c2f with a float, but it expects an int. Can you fix the problems?

For the curious: This error is just a warning because C/C++ lets you get away with things like this -- it will automatically convert the float to an int. In some other languages, this is a true error and not just a warning. Try running the result. It works, except that it truncates the input to an integer. (Note also that it uses integer division for c/5, i.e., it truncates that intermediate result, too.)

For the curious: This warning also says that main returns an int, even though we said it returns a void. What's going on? Every program returns an error code, which is an integer, describing whether an error occurred while running the program. We won't use this error code in our programs.

Copy and compile the program /home/comp210/public_html/Labs/lab10/error3.cc. It complains

error3.cc: In function `int main(...)':
error3.cc:10: `cin' undeclared (first use this function)
error3.cc:10: (Each undeclared identifier is reported only once
error3.cc:10: for each function it appears in.)
error3.cc:10: `c' undeclared (first use this function)
error3.cc:11: `cout' undeclared (first use this function)
error3.cc:11: `endl' undeclared (first use this function)
I.e., there are a bunch of things which haven't been properly been declared/defined/introduced. Look at the program -- can you identify what these errors are complaining about and what caused them?

Copy and compile the program /home/comp210/public_html/Labs/lab10/error4.cc. It complains

error4.cc: In function `int main(...)':
error4.cc:12: no match for `operator <<(class _IO_istream_withassign, float)'
I.e., in the function main, on line 12, there is no definition of << appropriate for its use. Can you find the error?

For the curious: This is basically shows that input stream cin and output stream cout are not of the same type. Furthermore, the message shows that cin is an object, as its type is a certain class. Although we won't be introducing C++'s objects, we are actually using some of the predefined ones.

Ok, now for the big event. Copy /home/comp210/public_html/Labs/lab10/error5.cc to your directory. Compile it -- it reports an error, so you need to fix it. Now try compiling it -- it reports some more errors! The first time around, the compiler became sufficiently confused by the first error that it gave up looking for any more. Now that you've fixed that, it looked for and found more errors. You need to fix these. Repeat until you get a working (if silly) program.

All of these errors have been found during compilation. Give what we have seen of C/C++ so far, it's pretty hard to have a run-time error. (This ignores the possibility that your program may compute something correctly, just not the right thing.)

For the curious

Let's look at some possible run-time errors given what we've seen so far. Look at and compile /home/comp210/public_html/Labs/lab10/sum.cc. This will sum the squares of all integers in an interval. Run the compiled program:

Look at and compile /home/comp210/public_html/Labs/lab10/ack.cc. This is a funky function called Ackerman's function. The result of this can easily be a really big number, so we use floats instead of ints. Run the compiled program: