Skip to content

C tips for Fortran Ocean Modellers

Since HIM is written in C, and most other ocean GCMs are written in Fortran, the following tips might be useful for quickly getting started with HIM. I could name a half dozen people who have managed to get meaningful (and customized) simulations running with HIM within a day. So don’t be intimidated by the language barrier!

  1. Trying poking around the model to see if it makes sense to you; most syntax is decipherable by someone with Fortran programming experience. It might be good to spend about a day skimming through a good tutorial – there are many, but I like “Practical C Programming” by Oualline (O’Reilly). But also check a decent, comprehensive reference manual – “C: A Reference Manual” by Harbison and Steele (Prentice-Hall) or the original “The C Programming Language” by Kernighan and Ritchie (Prentice-Hall) are widely available.
  2. C is case sensitive – x and X are not the same variable.
  3. C’s arrays occur in memory in the opposite order from Fortran. That is, X[k][j][i] is equivalent to X(i,j,k) in Fortran.
  4. C statements continue until a semicolon (;) is reached. Carriage returns have no more significance than do spaces, except when C++ style comments are used.
  5. Comments, such as “/* This is a comment. */”, can extend across many lines, but multiple comments cannot be nested. Many C compilers support C++ style comments, in which double slashes “//” comment out the remainder of a line. HIM uses C++ style comments in some files.
  6. C has a terse conditional assignment, which is sometimes used in HIM. The following two lines do the same thing, although performance may be better with the first:
    a = (b > c) ? d : e;
    if (b > c) a = d; else a = e;
    
  7. Pointer referencing and dereferencing is the subject of entire chapters in decent books. Structures like *a, &a and a->var are all related to this subject, and if you need to understand them a C reference will need to be consulted.
  8. Variables can be defined at the beginning of any block, which are delimited by braces, { }, but are only available within that block. It is considered good programming practice to limit the “scope” of a variable as much as possible – i.e. to define it in the smallest block that makes sense.
  9. “extern” is used to refer to variables that are defined in another file outside of any block. (C has a single external variable namespace, while C++ permits naming of namespaces, which is much like Fortran90 modules.) There is no distinction between “functions” and “subroutines” in C. But note that any scalar variables passed to a function are passed as copies. If you want the external value of a variable to be changed by a function, you must pass a pointer to that variable (i.e. its address in memory). Arrays are already treated like pointers when passed as subroutine arguments and their external values can be changed in a function.

If these tips are not clear, or there are other quick pointers that you feel would be helpful to include on this list, e-mail Bob Hallberg
– Robert.Hallberg@noaa.gov.