gfortran/g77 and 32-bit Integer

485 views
Skip to first unread message

albert.th...@gmail.com

unread,
Jul 10, 2007, 12:12:35 PM7/10/07
to GNU Fortran
My question is twofold when I think about it.

1.) what switches do I apply for gfortran for it do behave as g77 i.e
want my program t(I guess what Ia am saying is that I want my program
to be according to the fortran 77 specification).

2.) How do I declare an Integer variable to be a 32-bit integer (Using
the watcom compilator I usually declare them by writing Integer*4)

Fortran isn't my mother tung so I would be a very happy hacker if
someone out there could shed some light on this. ;)

Thank you for your help!

Tobias Burnus

unread,
Jul 11, 2007, 3:46:34 AM7/11/07
to GNU Fortran
Hello,

On Jul 10, 6:12 pm, "albert.thuswald...@gmail.com"


<albert.thuswald...@gmail.com> wrote:
> 1.) what switches do I apply for gfortran for it do behave as g77 i.e
> want my program t(I guess what Ia am saying is that I want my program
> to be according to the fortran 77 specification).

As the Fortran 90/95/2003 standard is almost completely compatible
with Fortran 77 standard (very few elements have been deleted), using
any -std= option should work, especially the default options should
work. If it does not, you could try -std=legacy.

If it does not work, please drop a mail or enter a bugreport. (Please
use at least gfortran 4.1, better 4.2.)

> 2.) How do I declare an Integer variable to be a 32-bit integer (Using
> the watcom compilator I usually declare them by writing Integer*4)

The default integer is already 32 thus "integer" is enough, otherwise
using 4 (32 bits), 8 (64) and either 10 (80) or 16 (128) works.

Tobias

PS: I think there are relatively few persons subscribed to this google
list; using the fortran <at> gcc.gnu.org will probably reach more
persons.

albert.th...@gmail.com

unread,
Jul 12, 2007, 9:41:29 AM7/12/07
to GNU Fortran

Thank you for your reply Tobias.

Through your answer and some head-scratching of my own I think I
understand the subject i bit better.

A follow up question to point 2.)

2.1) What's the standard way to declare let's say a 16-bit integer:

Integer*2
or
Integer (KIND=2)

2.2) Can I be sure that Integer is 32-bit is default on all
platforms?


Tobias Burnus

unread,
Jul 12, 2007, 10:25:11 AM7/12/07
to GNU Fortran
Hello,

On Jul 12, 3:41 pm, "albert.thuswald...@gmail.com"


<albert.thuswald...@gmail.com> wrote:
> > > 2.) How do I declare an Integer variable to be a 32-bit integer (Using
> > > the watcom compilator I usually declare them by writing Integer*4)
>

> 2.1) What's the standard way to declare let's say a 16-bit integer:
> Integer*2
> or
> Integer (KIND=2)

The first is Fortran 77 syntax the second Fortran 90/95/2003 syntax.
(Note, exception: complex*16 is the same as complex(kind=8).)

This should work with almost all compilers, but is not the standard-
defined way of doing as the 1,2,4,8,16 are not defined by the
standard; to be standard conform you have to use
"selected_real_kind()", example:

integer, parameter :: i2 = selected_real_kind(4)
integer(i2) :: myInt
myInt = 42_i2

The "42_i2" makes sure that the "42" is an integer(kind=i2); for most
compilers i2 has the value 2 (16 bits are enough to represent the
numbers between -10^4 to 10^4 (-10000 to 10000).

> 2.2) Can I be sure that Integer is 32-bit is default on all
> platforms?

Well, I think the majority of compilers have 32bit by default, but the
user can overwrite it using e.g. using "-r8" or as in gfortran with "-
fdefault-integer-8". If you want to make sure you have 32bit you
should specify the kind value.

Below you find the output of
print *, huge(1_1) to huge(1_16)
and selected(i=1...):

huge(1_1) = 127
huge(1_2) = 32767
huge(1_4) = 2147483647
huge(1_8) = 9223372036854775807
huge(1_16) = 170141183460469231731687303715884105727
i = 0 10**i = 1 kind = 1
i = 1 10**i = 10 kind = 1
i = 2 10**i = 100 kind = 1
i = 3 10**i = 1000 kind = 2
i = 4 10**i = 10000 kind = 2
i = 5 10**i = 100000 kind = 4
i = 6 10**i = 1000000 kind = 4
i = 7 10**i = 10000000 kind = 4
i = 8 10**i = 100000000 kind = 4
i = 9 10**i = 1000000000 kind = 4
i = 10 10**i = 10000000000 kind = 8
i = 11 10**i = 100000000000 kind = 8
i = 12 10**i = 1000000000000 kind = 8
i = 13 10**i = 10000000000000 kind = 8
i = 14 10**i = 100000000000000 kind = 8
i = 15 10**i = 1000000000000000 kind = 8
i = 16 10**i = 10000000000000000 kind = 8
i = 17 10**i = 100000000000000000 kind = 8
i = 18 10**i = 1000000000000000000 kind = 8
i = 19 10**i = 10000000000000000000 kind = 16
i = 20 10**i = 100000000000000000000 kind = 16
i = 21 10**i = 1000000000000000000000 kind = 16
i = 22 10**i = 10000000000000000000000 kind = 16
i = 23 10**i = 100000000000000000000000 kind = 16
i = 24 10**i = 1000000000000000000000000 kind = 16
i = 25 10**i = 10000000000000000000000000 kind = 16
i = 26 10**i = 100000000000000000000000000 kind = 16
i = 27 10**i = 1000000000000000000000000000 kind = 16
i = 28 10**i = 10000000000000000000000000000 kind = 16
i = 29 10**i = 100000000000000000000000000000 kind = 16
i = 30 10**i = 1000000000000000000000000000000 kind = 16
i = 31 10**i = 10000000000000000000000000000000 kind = 16
i = 32 10**i = 100000000000000000000000000000000 kind = 16
i = 33 10**i = 1000000000000000000000000000000000 kind = 16
i = 34 10**i = 10000000000000000000000000000000000 kind = 16
i = 35 10**i = 100000000000000000000000000000000000 kind = 16
i = 36 10**i = 1000000000000000000000000000000000000 kind = 16
i = 37 10**i = 10000000000000000000000000000000000000 kind = 16
i = 38 10**i = 100000000000000000000000000000000000000 kind = 16

Tobias

Reply all
Reply to author
Forward
0 new messages