Information Technology Reference
In-Depth Information
code. Every statement ends with a semi-colon, and one can have as many statements
asdesiredonthesameline.InC CC , variables can be declared wherever they are
needed. The loop is implemented via the for construction, which is adopted in a
wide range of programming languages.
A test program, calling up the Trapezoidal function to perform the numerical
integration of a specific function, can take the form
#include <iostream>
#include <cmath>
/ * the Trapezoidal function: * /
...
/ * test function to be integrated * /
double f1 (double x)
{
return exp(-x * x) * log(1+x * sin(x));
}
int main() // main program
{
double a = 0,b=2;intn=1000;
double result = Trapezoidal (a, b, f1, n);
std::cout << result << std::endl;
}
The #include statements include information about functions in libraries (here
input/output functionality from iostream and mathematical functions from cmath );
the compiler needs to see a declaration of a function, i.e. the type of return value
and the type of arguments, before a call to the function can be compiled. For exam-
ple, calling the exponential math function exp requires us to include the cmath file
where exp is declared. If you call the function with the wrong types of arguments, a
compiler error will be issued. Fortran 77 has no information about the functions one
calls, so if you provide the wrong type or wrong number of arguments, the call will
compile normally, but the run-time behavior is unpredictable. Very often strange
things happen, and it requires some experience to find such bugs. Many program-
mers switched from Fortran 77 to C or C CC just because the C and C CC compilers
find so many more coding errors.
The code segments above are written in C CC , but the C version is quite similar.
Since C CC has C as a subset and is a more flexible computer language, the authors
recommend using C CC instead of plain C. Some constructs in C CC , especially
related to programming with classes, can result in a slight performance loss. One
can, nevertheless, often rewrite time-critical parts in C CC programs using the C
subset in order to take advantage of the performance of C.
A very popular C compiler is gcc (GNU's C compiler). On most Unix sys-
tems, the vendor's compiler has the name cc .TheC CC counterpart to gcc is g++ .
This is a good all-round compiler, but some commercial compilers, especially the
KCC compiler, can give significantly better performance in scientific computing
applications.
Search WWH ::




Custom Search