2010-07-03 03:47:23 8 Comments

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)`

### Related Questions

#### Sponsored Content

#### 23 Answered Questions

### [SOLVED] Is Fortran easier to optimize than C for heavy calculations?

**2008-09-28 16:02:52****quinmars****126944**View**381**Score**23**Answer- Tags: c performance fortran

#### 6 Answered Questions

### [SOLVED] changing array dimensions in fortran

**2011-03-23 13:33:53****steabert****24028**View**13**Score**6**Answer- Tags: fortran

#### 2 Answered Questions

#### 1 Answered Questions

### [SOLVED] Converting Fortran to Javascript

**2016-07-05 09:42:53****Kamal****706**View**2**Score**1**Answer- Tags: javascript compiler-construction fortran interpreter transpiler

#### 1 Answered Questions

### [SOLVED] Do Fortran kind parameter for a particular precision change depending on the processor even with the same compiler?

**2016-04-27 01:36:10****Kun****70**View**0**Score**1**Answer- Tags: fortran

#### 0 Answered Questions

#### 3 Answered Questions

### [SOLVED] Using underscores to define kind/precision

**2015-05-20 23:54:41****Ross****1418**View**5**Score**3**Answer- Tags: fortran

#### 1 Answered Questions

### [SOLVED] Does fortran function pass by const reference?

**2014-12-12 21:44:41****andy90****233**View**2**Score**1**Answer- Tags: fortran

#### 2 Answered Questions

### [SOLVED] Fortran subroutine value keyword

**2013-09-30 17:59:30****Zuoshu Lu****611**View**2**Score**2**Answer- Tags: fortran subroutine

## 3 comments

## @Rodrigo Rodrigues 2018-10-14 06:58:10

I will make reference to this enlightening article, wrote recently by @SteveLionel, and try cover some details that are not present in the other answers so far:

`integer*n`

or`real*n`

was a common extension provided by compilers long time ago, when different computer architectures started to have different designs for in-memory format of integer and real values, where`n`

was thesize in bytesof the value stored. However, that said nothing about range or precision of those values: different implementations of a 16bit integer, for example, could provide different ranges and limit values.When Fortran 90 came out,

`kind`

parameters were added to the language, along with intrinsic inquiry functions (specially`kind`

,`selected_int_kind`

and`selected_real_kind`

, but also others, like`precision`

,`digits`

,`epsilon`

...) to aid the programmer to specify minimun requirements forprecision and rangeof numeric types (still, no official mention to storage model or bytes). The syntax is`integer(kind=n)`

or even`integer(n)`

, where`n`

is a constant value corresponding to a kind of integer supported by the compiler. For literal constants, the syntax is`12_n`

or`3.4e-2_n`

.The advantage of this solution was that Fortran didn't (and still don't) make any assumptions about the implementation details of data-types other than the results of the inquiry functions used to choose the type, so the code is parameterized by the problem being solved, not by the language or the hardware. The gotcha is, as said in other answers, each compiler can choose their kind numbers, thus assuming magic number like

`integer(4)`

is not portable.default kinds, that is what you get when you don't specify a kind.`ieee_arithmetic`

, you can inquire and select a real type with IEEE floating point capabilities, if avaliable.`iso_fortran_env`

intrinsic module, that had functions to inquire thestorage sizeof the types implemented by a compiler, with intrinsics like`numeric_storage_size`

and`bit_size`

. Another addition of Fortran 2003 revision was the`iso_c_binding`

intrinsic module, that provided kind parameter values to guarantee compatibility with C types, in storage, precision and range.`iso_fortran_env`

to include named constants`int8`

,`int16`

,`int32`

m`int64`

,`real32`

,`real64`

and`real128`

, whose values correspond to the kinds of integer and real kinds that occupy the stated number of bits. The gotcha is that those constantsonly assure storage size, not precision or range. Only use them when this is exactly what you want.## @M. S. B. 2010-07-03 05:41:19

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: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. Although, this syntax isn't and was never standard Fortran.`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.

UPDATE: if you don't want to specify numeric types by the required precision, but instead by the storage that they will use, Fortran 2008 provides a method. reals and integers can be specified by the number of bits of storage after

`use`

ing the`ISO_FORTRAN_ENV`

module, for example, for a 4-byte (32-bit) integer:The gfortran manual has documentation under "intrinsic modules".

## @Sam 2010-07-03 21:09:01

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

## @Vladimir F 2014-07-24 07:41:41

Just one more explicit explanation what the kind is. The compiler has a table of different numerical types. All integer types are different

kindsof the basic type --`integer`

. Let's say the compiler has 1 byte, 2 byte, 4 byte, 8 byte and 16 byte`integer`

(or`real`

) kinds. In the table the compiler has an index to each of this kind -- this index is the kind number.Many compilers choose this numbering:

But they can choose any other numbering. One of the obvious possibilities is

There are indeed compilers (at least g77 and NAG) which choose this approach. There are also options to change this. Therefore

`kind`

numbers are not portable`integer(kind=4)`

or`integer(4)`

means a 4 byte integer or a 8-bytes integer depending on the compiler.`integer*4`

is portable in the sense it always means 4 bytes. But on the other hand it is not portable because it has never been part of any standard. Programs using this notation are not valid Fortran 77, 90 or any other Fortran.To see the right options how to set the kind numbers see M.S.B.'s answer.

The same concept holds for

`real`

data types. See Fortran 90 kind parameter (the mataap's answer).## @syscreat 2016-07-12 07:34:14

The interesting thing here, is that forms type(kind) and type*kind are not fully interchangeable and it's requires attention. For example, in Intel Fortran integer(6) and integer*4 are the same, but: COMPLEX([KIND=]4) is the same as COMPLEX*8, COMPLEX([KIND=]8) or COMPLEX*16, COMPLEX([KIND=]16) or COMPLEX*32

## @Vladimir F 2016-07-12 07:51:19

@syscreat I did not address real and complex in this answer, because there is a separate question about them. I addressed only integer here. See the link at the and of the answer. The fact that real and complex of the same precision have the same kind number is forced by the standard.