# Some explanation needed about kind example

**URL:** https://fortran-lang.discourse.group/t/some-explanation-needed-about-kind-example/7891
**Category:** Uncategorized
**Created:** [April 22, 2024, 5:49pm UTC](https://fortran-lang.discourse.group/t/some-explanation-needed-about-kind-example/7891 "2024-04-22T17:49:26Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![rmoortgat](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/rmoortgat/32/3569_2.png) [@rmoortgat](https://fortran-lang.discourse.group/u/rmoortgat)
#### Post date: [April 22, 2024, 5:49pm UTC](https://fortran-lang.discourse.group/t/some-explanation-needed-about-kind-example/7891/1 "2024-04-22T17:49:26Z")

</div>

This is an example from Ian Chivers book:

```auto
program ch0509

  implicit none

! example of the use of the kind function
! and the numeric inquiry functions
! for integer kind types

! 8 bit -128 to
! 127 10**2

! 16 bit -32768 to
! 32767 10**4

! 32 bit -2147483648 to
! 2147483647 10**9

! 64 bit
! -9223372036854775808 to
! 9223372036854775807 10**18

  integer :: i
  integer, parameter :: i8 = selected_int_kind(2)
  integer, parameter :: i16 = selected_int_kind(4)
  integer, parameter :: i32 = selected_int_kind(9)
  integer, parameter :: i64 = selected_int_kind(18)
  integer (i8) :: i1
  integer (i16) :: i2
  integer (i32) :: i3
  integer (i64) :: i4

  print *, ' '
  print *, ' integer kind support'
  print *, ' kind huge'
  print *, ' '
  print *, ' ', kind(i), ' ', huge(i)
  print *, ' '
  print *, ' ', kind(i1), ' ', huge(i1)
  print *, ' ', kind(i2), ' ', huge(i2)
  print *, ' ', kind(i3), ' ', huge(i3)
  print *, ' ', kind(i4), ' ', huge(i4)
  print *, ' '
end program

```

An integer i is declared but not initialized. On my system it gets initialized to 0 although afaik this is not mandatory.  
Now the output with ifort compiler gives:

```auto

  integer kind support
  kind huge

             4 2147483647

             1 127
             2 32767
             4 2147483647
             8 9223372036854775807

```

It looks like kind(i) in this case kind(0) turns out to be 4.  
I suppose there is a perfectly logical explanation but as I’m afraid I don’t see it.

Roger

---

<div class="post-metadata">

### Author: ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)
#### Post date: [April 22, 2024, 6:08pm UTC](https://fortran-lang.discourse.group/t/some-explanation-needed-about-kind-example/7891/2 "2024-04-22T18:08:26Z")

</div>

The kind of a variable is known from its declaration, even if it has not been set. A declaration

`integer :: i`

means that `i` is of the type default integer, and most compilers use `4` for the kind number of default integers, although this should not be relied upon.

---

<div class="post-metadata">

### Author: ![rmoortgat](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/rmoortgat/32/3569_2.png) [@rmoortgat](https://fortran-lang.discourse.group/u/rmoortgat)
#### Post date: [April 22, 2024, 6:33pm UTC](https://fortran-lang.discourse.group/t/some-explanation-needed-about-kind-example/7891/3 "2024-04-22T18:33:54Z")

</div>

Thank you.

Verzonden vanaf [Outlook voor Android](https://aka.ms/AAb9ysg)

---

<div class="post-metadata">

### Author: ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)
#### Post date: [April 22, 2024, 10:46pm UTC](https://fortran-lang.discourse.group/t/some-explanation-needed-about-kind-example/7891/4 "2024-04-22T22:46:13Z")

</div>

> [@rmoortgat](#):
>
> An integer i is declared but not initialized. On my system it gets initialized to 0 although afaik this is not mandatory.

The value of `i`, which is declared as a default integer, is not needed in the program, only its type and kind are required. Values are also not assigned or initialized to `i1`, `i2`, `i3`, and `i4`, and that is allowed for the same reason. On most compilers, there are options that allow the default kind to be changed, in which case the first line of the output would be different and would presumably match one of the other output lines.

---

<div class="post-metadata">

### Author: ![rmoortgat](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/rmoortgat/32/3569_2.png) [@rmoortgat](https://fortran-lang.discourse.group/u/rmoortgat)
#### Post date: [April 23, 2024, 7:11am UTC](https://fortran-lang.discourse.group/t/some-explanation-needed-about-kind-example/7891/5 "2024-04-23T07:11:43Z")

</div>

OK, thank you.

This kind concept in fortran is a bit confusing for a beginner.

---

<div class="post-metadata">

### Author: ![PierU](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pieru/32/1848_2.png) [@PierU](https://fortran-lang.discourse.group/u/PierU)
#### Post date: [April 23, 2024, 8:41am UTC](https://fortran-lang.discourse.group/t/some-explanation-needed-about-kind-example/7891/6 "2024-04-23T08:41:48Z")

</div>

> [@rmoortgat](#):
>
> An integer i is declared but not initialized. On my system it gets initialized to 0 although afaik this is not mandatory.

Note that your variables `i1`, …, `i4` are not explicitly initialized either, and are probably equal to zero as well. Yet `kind(i1)...` return what you are expecting. In `kind(<variable_or_constant>)`, the value of the variable of constant doesn’t matter.

---

<div class="post-metadata">

### Author: ![rmoortgat](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/rmoortgat/32/3569_2.png) [@rmoortgat](https://fortran-lang.discourse.group/u/rmoortgat)
#### Post date: [April 23, 2024, 8:57am UTC](https://fortran-lang.discourse.group/t/some-explanation-needed-about-kind-example/7891/7 "2024-04-23T08:57:00Z")

</div>

Thanks! Got it.
