FORTRAN versus C

This comparison of FORTRAN and C eloquently exposes deficiencies in the original C language. Happily some of them have been redressed.

I speak hardly any FORTRAN so I must rely on the descriptions in the document. I may have misinterpreted them.

Variable-dimension array arguments

A killer feature of FORTRAN. C90 workarounds are terrible. Fortunately, C99 rescues us:

void vector_add(int n, int x[n], int a[n], int b[n]) {
  for(int i = 0; i < n; i++) x[i] = a[i] + b[i];
}

If the dimension of the vectors should be last:

void vector_add(int n; int x[n], int a[n], int b[n], int n) {
  for(int i = 0; i < n; i++) x[i] = a[i] + b[i];
}

This feature is also a GNU C extension.

Generic-precision intrinsic functions

The C99 committee listened to FORTRAN programmers and introduced <tgmath.h>, a type-generic edition of <math.h>. With this header, we no longer have to choose wisely from log, logf, logl, … We simply write log.

Complex arithmetic

C99 introduced a complex data type. I wonder how it compares.

Array index ranges other than [0..N-1]

I hate this limitation of C. Luckily, most of the time it doesn’t bother me.

Implied do

GNU C nested functions seem at least as powerful, though it is a shame there is no notation for anonymous functions. But yes, standard C is lacking here.

Infix exponentiation operator

I’m jealous of this feature, though I can tolerate workarounds. Observe we need more than exp, expf, expl: we also require a routine tuned for the case when the base is a float and the exponent is an integer.

FORTRAN 90 array notation

Workarounds in C are less pleasant but still tolerable.

No explicit pointers

Pointers can hamper compiler optimization, but they can also help. Hopefully, the restrict keyword of C99 will alleviate the thornier problems with explicit pointers.

Static allocation encouraged

Except for inner loops, the savings are slight. However, C programmers should be aware that some situations call for replacing dynamic allocation with static allocation.

Pointer aliasing

The C99 restrict feature levels the playing field in theory, but FORTRAN might have the upper hand because all pointers are restricted by default, and FORTRAN programmers are more conscious of the difference between restricted and explicit pointers.

Long double precision

Another feature that was at last addressed with C99. Also, GNU C supports 80-bit and 128-bit floating point types.

No confusion between x, &x, *x

This is indeed confusing for beginners. The size 1 array trick can take some of the pain away, but ultimately a C programmer must acquire familiarity with pointers and memory addresses.

I would have also mentioned x[]. This is perhaps even more perplexing, as its meaning is context-dependent. I wonder if this is necessary for maximum performance. Would C be less efficient with syntactic separation of arrays and pointers?

Lesser issues

The documents lists other FORTRAN advantages, but I found these less compelling. In fact, some seem like FORTRAN disadvantages, as I appreciate case sensitivity, reserved words, recursion, and a smaller standard library.

I don’t understand why being able to pass all variables by reference is touted as a benefit. Why would it generate more efficient assembly than passing pointers around?


Ben Lynn blynn@cs.stanford.edu