Does 64-bit integer literals need a suffix?

Since 64-bit real literals need a suffix d, does 64-bit integer literals need a suffix?

integer(8) :: x
x = 1
2 Likes

Yes. This code does not even compile without the kind suffix,

program hello
    use iso_fortran_env, only: int64
    print *, 99999999999
    print *, 99999999999_int64
end program hello

Try it here.

1 Like

An integer without a suffix has the default integer type. Assigning it to a 64-bit integer will work if and only if the integer is in the range of the default integer. The program below compiled with gfortran -fno-range-check i64.f90

use iso_fortran_env, only: int64
integer(kind=int64) :: i1,i2
integer             :: i3,i4
i1 = 1234567890123_int64
i2 = 1234567890123
i3 = 1234567890123_int64
i4 = 1234567890123
print*,i1,i2,i3,i4
i1 = 123456_int64
i2 = 123456
i3 = 123456_int64
i4 = 123456
print*,i1,i2,i3,i4
end

gives

        1234567890123        1234567890123  1912276171  1912276171
               123456               123456      123456      123456

However, the option -fno-range-check was used only for illustration and should not be used for real work. With the default options, gfortran says

i64.f90:5:18:

    5 | i2 = 1234567890123
      |                  1
Error: Integer too big for its kind at (1). This check can be disabled with the option '-fno-range-check'
i64.f90:7:18:

    7 | i4 = 1234567890123
      |                  1
Error: Integer too big for its kind at (1). This check can be disabled with the option '-fno-range-check'
i64.f90:6:5:

    6 | i3 = 1234567890123_int64
      |     1
Error: Arithmetic overflow converting INTEGER(8) to INTEGER(4) at (1). This check can be disabled with the option '-fno-range-check'
1 Like