FORTRAN versus C


This page is intended as a quick conversion guide for programmers who already know FORTRAN and who want to convert a program to C (or even vice versa). It is NOT intended to be complete, so beware!

ItemFORTRANC
Overall structure
                                
  program xxx
  implicit none
  ....
  ....
  ....
  end
  #include ...
                                
  main() {
      ....
      ....
      ....
  }
Case sensitive?
  
  no (by default)
  
  yes
Data types
  
  character
  integer
  logical
  real
  double precision
  
  char
  int
   - (int)
  float
  double
Delimiters
  
  newline
  
  { } ; ,
Strings
  
  'I am a string'
  
  "I am a string"
Comments
  
  "c" in column 1
  .... ! ..(comment)..
  
  /* ..(comment).. */
  
Arithmetic
  
  a = b + c*d
  i = i + 1
  x = x + y
  
  a = b + c*d;
  i = i + 1;  or  i++;
  x = x + y;  or  x += y;
Constants
  
  parameter (PI = 3.14)
  
  #define PI 3.14
Conditionals
  
  if (expression1) then
      ....
      ....
  else if (expression2) then 
      ....
      ....
  else
      ....
      ....
  end if
  
  if (expression1) {
      ....
      ....
  } else if (expression2) {
      ....
      ....
  } else {
      ....
      ....
  }
Logical operators
  
  .eq.
  .ne.
  .gt.
  .lt.
  .ge.
  .le.
  .and.
  .or.
  .not.
  
  ==
  !=
  >
  <
  >=
  <=
  &&
  ||
  !
Loops
  
  do i = i1, i2, inc
      ....
      ....
  end do

  do while (expression)
      ....
      ....
  end do
  
  for (i=i1; i<=i2; i+=inc) { 
      ....
      ....
  }

  while (expression) {
      ....
      ....
  }
Functions
  
  real function func(x)

  ....
  ....

  func = real-expression
  return
  end

  subroutine sub(a, b)

  ....
  ....

  b = expression
  return
  end
  
  float func(float x)
  {
      ....
      ....

      return float-expression;

  }

  void sub(int a, int * b)
  {
      ....
      ....

      *b = expression;

  }
I/O
  
  write(*, format) x,y,z
  read (*, format) x,y,z
  
  printf(format, x, y, z);
  scanf(format, &x, &y, &z);
I/O format
  
  '(a,1x,i,1x,f)'
  
  "%s %d %f\n"
File I/O
  
  integer unit
  open(unit, file='xxx')
  write(unit,format)data
  read(unit,format)data
  close(unit)
  
  FILE * fp;
  fp = fopen("xxx", "w");
  fprintf(fp, format, data);
  fscanf(fp, format, &data); 
  fclose(fp);
Command line

  character*80 arg
  n_args = iargc()
  call getarg(i, arg)


  read(arg, format)data
  main(int argc, char* argv[])



  int j = atoi(argv[i]);
  float x = atof(argv[i]);
  sscanf(argv[i], format, &data);