Using C and C++

CSci 5106: Programming Languages

You have a choice of compilers that you can use for C and C++ in the local environment: the GNU versions in gcc and g++ respectively and what I believe are the SUN supplied versions in cc and c++ respectively. I prefer to use the the GNU versions and I will do this in the notes below. You are, of course, free to choose. If the module soft/egcs/3.0 is loaded, then you will be able to type the names for the gnu versions at the command level.

One specific point to note for this course: when I say you should write a program in C, I really mean C and not C++. In particular, I want you not to be using classes, operator overloading and other such features that are squarely a part of C++. I am hoping we will have a chance to talk about object-oriented aspects in this class, at which point class structure will be acceptable, but not before that. Overloading can have nasty characteristics if you use it without thinking and, to avoid this, I want you not to use it at all in the first instance. I would also like you to be using only the C input-output facilities, but I will tolerate your using the C++ insertion operator (<<)and stream based ideas if you feel you absolutely must.

To illustrate the use of the C compiler, let us consider the following C program:

#include <stdio.h> int power(int x, int n) { int i, p; p = 1; for (i = 1; i <= n; ++i) p = p * x; return(p); } int main() { int i; for(i=0; i < 10; i++) printf("%2d %5d %6d\n", i, power(2, i), power(-3, i)); }

Assume that this program is saved in a file with name power.c in a directory currently in your path. You can then compile this program using the following command:

% gcc power.c

(The % symbol is the system prompt.) The compiler will then process the file power.c, providing you with information as it goes along, some of which might be diagnostic in nature. If compilation is successful, as it should be in this case, an executable version will have been created in the file a.out. You can run this as follows:

% a.out 0 1 1 1 2 -3 2 4 9 3 8 -27 4 16 81 5 32 -243 6 64 729 7 128 -2187 8 256 6561 9 512 -19683 %

Sometimes you may want to produce an executable in a file with a more descriptive name than a.out. You can force this by using the option -o with gcc. For instance, the command

% gcc -o power.o power.c

will put the executable in the file power.o instead of in a.out. The compiler has other switches that you can find out about by looking up the man page for it.

When you start writing programs in many different languages, it will be useful to adopt certain conventions in naming files. In this regard you may note that c is generally the extension to use for C programs and cpp for C++ programs. There are also some differences between how gcc, the same compiler, treats a program in a file named power.c and in a file named power.cpp—it seems to require stricter syntax standards of one in a file with extension cpp—so the best bet is to follow this naming convention closely.

If you are not very familiar with the C Programming Language, there is an online manual. This would be difficult to use if you know nothing about the C language, but, if you know the general concepts of constructs and i/o procedures as I understand all of you do already, this manual seems to provide a quick way of checking out details. If you are looking for a book to purchase on C, I will recommend the Kernighan and Richie book, a reference to which appears in the main software page.