1-2 COMPARISON OF FORTRAN AND C ******************************** (Thanks to Craig Burley for the excellent comments) The world of computing sometimes adopts silly fashions, too often good companies and products fell from grace, and lesser ones gain the upper hand. It seems that the fashion winds (in the US, in the UK it seems to be different) blows now in the numerical computing world towards C and C++, this strange trend is probably driven by people who are not experienced numerical programmers. See the article by Dr. John Prentice: Fortran 90 as a language of choice for science students and the one by Jerrold Wagener: Fortran 90 and Computational Science Fortran still predominates in that area, but it seems to lose ground, the following points may help you make up your mind. (Partly adapted from the Fortran FAQ) a) FORTRAN tends to meet some of the needs of scientists better. Most notably, it has built in support for: - Variable-dimension array arguments in subroutines, this feature makes it possible to write general purpose routines without explicitly specifying array dimensions passed to them. Standard C lacks this important feature (some compilers like gcc have it as non-standard extension) and the workarounds are very cumbersome. This feature by itself is sufficient to prefer Fortran over C in numerical computing. - A rich set of useful generic-precision intrinsic functions. Such functions can be highly optimized (written in assembly language with optimized cache utilization), and they make programs standard at a higher level (and more portable). - Builtin complex arithmetic (arithmetic involving complex numbers represented as having real and imaginary components). - Array index-ranges may start at an arbitrary integer. - Better I/O routines, e.g. the implied do facility gives flexibility that C's standard library can't match. The Fortran compiler directly handles the more complex syntax involved, and as such syntax can't be easily reduced to argument passing form, C can't implement it efficiently. - A compiler-supported infix exponentiation operator which is generic with respect to both precision and type, AND which is generally handled very efficiently, including the commonly occurring special case floating-point**small-integer. - Fortran 90 supports an array notation that allows operations on array sections, and is suitable for parallel processing. - Fortran 90 supports automatic selection of numeric data types having a specified precision and range, and makes Fortran programs even more portable. - Fortran extensions for parallel programming are standardized by the High Performance Fortran (HPF) consortium. Fortran 90 supports useful features of C (column independent code, pointers, dynamic memory allocation, etc) and C++ (operator overloading, simple objects) and has in addition a very sophisticated capability to manipulate arrays. Fortran 90 added features facilitating numeric precision and range control, thus improving program portability. b) The design of FORTRAN allows maximal speed of execution: - FORTRAN 77 lacks explicit pointers, which is one reason that it is more amenable to automatic code optimization. This is very important for high-performance computing. Fortran 90 allows explicit pointers with some restrictions designed to lessen hindrance to automatic optimizations. - Fortran was designed to permit static storage allocation, saving the time spent on creating and destroying activation records on the stack every procedure call/return. Recursive procedures are impossible with static allocation, but can be simulated efficiently when needed. - Fortran implementations may pass all variables by reference, the fastest method. - Fortran disallows aliasing of arguments in procedure-call statements (CALL statements and FUNCTION references), all passed argument lists must have distinct entries. Fortran disallows also aliasing between COMMON (global) variables and dummy arguments. These restrictions allows better compiler optimizations. c) There is a vast body of existing FORTRAN code (much of which is publicly available and of high quality). Numerical codes are particularly difficult to port, scientific establishments usually do not have large otherwise idle programming staffs, etc. so massive recoding into any new language is typically resisted quite strongly. d) FORTRAN 77 tends to be easier for non-experts to learn than C, because its 'mental model of the computer' is much simpler. For example, in FORTRAN 77 the programmer can generally avoid learning about pointers and memory addresses, while these are essential in C. More generally, in FORTRAN 77 the difference between (C notation) x, &x, and often even *x is basically hidden, while in C it's exposed. Consequently, FORTRAN 77 is a much simpler language for people who are not experts at computer internals. Because of this relative simplicity, for simple programming tasks which fall within its domain, (say writing a simple least-squares fitting routine), FORTRAN 77 generally requires much less computer science knowledge of the programmer than C does, and is thus much easier to use. Fortran 90 changes the picture somewhat, the new language is very rich and complex, but you don't have to use or even know about all this complexity. e) The C standard requires only a basic double-precision mathematical library, and this is often what you get. The FORTRAN standard, on the other hand, requires single & double precision math, many vendors add quad-precision (long double, REAL*16) and provide serious math support. Single-precision calculations may be faster than double-precision calculation even on machines where the individual machine instructions takes about the same time because single-precision data is smaller and so there are less 'memory cache misses'. Quad-precision (long double) calculations are sometimes necessary to minimize roundoff errors. If you have only double-precision mathematical routines, the basic mathematical primitives will take up unnecessary CPU time when used in single-precision calculations and will be inexact if used with 'long double'. f) FORTRAN is designed to make numerical computation easy, robust and well-defined: 1) The order of evaluation of arithmetical expressions is defined precisely, and can be controlled with parentheses. 2) The implicit type declaration feature saves time/typing (however it makes your program vulnerable to annoying and hard to detect bugs). 3) Case insensitivity eliminates bugs due to 'miscased' identifiers. 4) The lack of reserved words in the language gives the programmer complete freedom to choose identifiers. 5) The one statement per line principle (of course continuation lines are allowed with a special syntax) makes programs more robust. 6) Making Blanks (space characters) are insignificant (except in character constants) also contributes to robustness of FORTRAN programs. g) Last but not least, FORTRAN compilers usually emit much better diagnostic messages. In summary, we can say that the difference between Fortran and C, is the difference between a language designed for numerical computations, and a language designed for other purposes (system programming). +---------------------------------------------+ | | | SUMMARY OF FORTRAN ADVANTAGES | | ============================= | | a) Scientifically oriented | | b) Better optimized code | | c) A lot of existing code | | d) Easier to learn | | e) More efficient mathematics | | f) Easier to use and more robust | | g) Better diagnostics | | | +---------------------------------------------+Return to contents page