Information Technology Reference
In-Depth Information
CC
6.3.3
C and C
The C language was invented in the beginning of the 1970s. Its main application
was implementation of the Unix operating system. The combination of Unix and C
became a dominating programming environment in the late 1980s. C is clearly a
more advanced programming language than Fortran 77, and some computational
scientists switched from Fortran 77 to C. C gives the program developer more flex-
ibility, normally at the cost of a slight loss in computational performance. C has the
same fundamental data types as Fortran 77, but offers a struct type, which can
hold any composition of fundamental data types and other struct s. This offers the
possibility of creating more advanced data types than in Fortran 77.
C CC was designed to offer classes and object-oriented programming as in the
famous SIMULA 67 language, but with C syntax and C as a subset. Several features
of languages with classes were removed to improve performance. Classes in C CC
extend C's struct such that user-defined types contain both data and functions
operating on the data. This provides much more flexible data types than the struct
in C. Applications that can benefit from sophisticated user-defined data structures,
and perhaps also the style of object-oriented programming, will normally be much
simpler to implement in C CC than in Fortran 77 or C. The applications will also
often be simpler to maintain and extend. However, C CC code can seldom compete
with Fortran 77 when it comes to computational speed, although the difference is
small. With some tweaking, C and C CC can on many occasions be as fast as, or
even slightly faster, than Fortran 77, but this depends on the type of algorithms and
the involved data structures.
CandC CC programs for Algorithm 6.2 look very similar since this is a very
simple problem. C CC has a slightly more readable syntax, so we present the
algorithm in that language first:
typedef double ( * fptr) (double x);
double Trapezoidal (double a, double b, fptr f, int n)
{
double h = (b-a)/double(n); double s = 0; double x = a;
for(inti=1;i<=n-1; i++) {
x=x+h;
s = s + f(x);
}
s = 0.5 * (f(a) + f(b)) + s;
return h * s;
}
CandC CC have, unfortunately, some constructs that make it somewhat difficult to
explain even very simple programs to the novice. For example, the Trapezoidal
function needs a function f.x/ as an argument, and this argument must be a function
pointer, here called fptr and defined through a typedef statement. The rest of the
code should be easier to understand. Real variables with double precision ( real * 8 in
Fortran 77 terminology) are called double in C and C CC .The Trapezoidal func-
tion returns a double (the approximation to the integral). Contrary to Fortran 77,
the type of each argument precedes the argument name, providing a more compact
Search WWH ::




Custom Search