tags:

views:

67

answers:

2

Hi,

I'm trying to learn Fortran and I'm seeing a lot of different definitions being passed around and I'm wondering if they're trying to accomplish the same thing. What is the difference between the following?

  • integer*4
  • integer(4)
  • integer(kind=4)

Thanks

+3  A: 

In Fortran >=90, the best approach is use intrinsic functions to specify the precision you need -- this guarantees both portability and that you get the precision that you need. For example, to obtain integers i and my_int that will support at least 8 decimal digits, you could use:

integer, parameter :: RegInt_K = selected_int_kind (8)
integer (kind=RegInt_K) :: i, my_int

Having defined "RegInt_K" (or whatever name you select) as a parameter, you can use it throughout your code as a symbol. This also makes it easy to change the precision.

Requesting 8 or 9 decimal digits will typically obtain a 4-byte integer.

integer*4 is an common extension going back to old FORTRAN to specify a 4-byte integer.

integer (4) or integer (RegInt_K) are short for integer (kind=4) or integer (kind=RegInt_K). integer (4) is not the same as integer*4 and is non-portable -- the language standard does not specify the numeric values of kinds. Most compilers use the kind=4 for 4-byte integers -- for these compilers integer*4 and integer(4) will provide the same integer type -- but there are exceptions, so integer(4) is non-portable and best avoided.

The approach for reals is similar.

M. S. B.
that was a fabulous explanation! stunning! excellent! it's crystal clear to me now.. :D thank you very much!
Sam