| [ CIS 330 C/C++ Home Page | Syllabus | Assignments | Announcements ] | Last updated 2008/01/11 11:24:22 |
This page is a simple introduction to using the command line C/C++ programming environment on the CIS department systems. There are various graphical IDEs (Integrated Development Environments) for many platforms, e.g., Eclipse, XCode for Mac, Visual C++ for PCs. However, for the purposes of this class, editing source code with your favorite editor and compiling with the command line tools will be sufficient.
% cc -o outputFileName sourceFileName or
% gcc -o outputFileName sourceFileName
where outputFileName is the name of the binary executable to be produced and sourceFileName is the name of the file containing the source code. Conventionally, C source files are named with the suffix .c.
% CC -o outputFileName sourceFileName or
% g++ -o outputFileName sourceFileName
C++ source files are often named with suffixes .c, or .C, or .cc, or .cpp.
% outputFileName or
% ./outputFileName
That is, simply typing the name of the output file created will execute it. If your PATH does not include the current directory, then you can explicitly execute using the path name as shown in the second example.
int main(int argc, char *argv[])
{
...
}
It is expected that main returns 0 on successful termination or some non zero
error code on unsuccessful termination.
You can also declare main as void, although you may get a warning.
Command-line arguments are given in a similar way as in Java, but C/C++ use a pointer to a string of characters instead of a String object type. There is no such thing as a length field in arrays in C/C++, hence the need for passing the array length as argc. The command line arguments include the executable command name as argv[0]. You may also leave off the arguments for main if you have no need for command line argument values. (Java requires the formal argument even if you don't use it.)