Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

default integer values

Stuart_M_
Beginner
2,771 Views
I know in past versions of Fortran that integers were given a default value of 1 if none was stipulated - is this still the case?

I have a situation where a variable is normally read from an input file, but in this case the file is never read and so no value is ever specified... At this point I'm assuming the default value is then used, but the variable's value is 0.

Thank you
0 Kudos
6 Replies
TimP
Honored Contributor III
2,771 Views
Quoting - stubaan
I know in past versions of Fortran that integers were given a default value of 1 if none was stipulated - is this still the case?

I have a situation where a variable is normally read from an input file, but in this case the file is never read and so no value is ever specified... At this point I'm assuming the default value is then used, but the variable's value is 0.
No, there's generally no reliable default, unless one is specified in the source code. The legacy way is by DATA. The options /Qsave /Qzero, which aren't compatible with /Qparallel or /Qopenmp, attempt to initialize to 0 where possible. I've never seen a compiler which had default initialization to 1, except where there was a linker option which allowed that value to be specified.
0 Kudos
DavidWhite
Valued Contributor II
2,771 Views
Quoting - tim18
No, there's generally no reliable default, unless one is specified in the source code. The legacy way is by DATA. The options /Qsave /Qzero, which aren't compatible with /Qparallel or /Qopenmp, attempt to initialize to 0 where possible. I've never seen a compiler which had default initialization to 1, except where there was a linker option which allowed that value to be specified.

It is always dangerous to assume ANY default initialization of variables - I believe that the standard does not require any - you should initialize all of your variables, and could (should) change the compiler options to alert you to variables that have been used before they have been set. Otherwise you cannot guarantee the reliability and portability of your code.
0 Kudos
Stuart_M_
Beginner
2,771 Views
Interesting. So what do you guys make of this?

"Recall that in Basic the default value of a numeric variable is always zero - that is, if you introduce a numeric variable but do not specify its value, Basic automatically gives it the value zero. In GNU Fortran the situation is more confused. A real variable with no value specified will be given a value - but usually a very small value that is not precisely zero, and sometimes a value that is not even close to zero. An integer is given the default value 1. This strange behavior is hardly ever a problem, as usually when the variable is eventually used in the program it is given an appropriate value by some assignment statement."

0 Kudos
jimdempseyatthecove
Honored Contributor III
2,771 Views

There is no (required) default initialization of variables in the Fortran language specification. You are strongly urged the rigorously test your program in /Debug configuration with checks for usage of uninitialized variables. This will shake out a lot of latent bugs in your code (also turn on array bounds checking and geninterfaces checkinterfaces).
Additionally use IMPLICIT NONE. This will pick up typographical errors (not all, but many).

Jim Dempsey



0 Kudos
anthonyrichards
New Contributor III
2,771 Views
While weare mentioning Basic, Ilike the way thatVisual Basic allows you to add 'Dim variable As whatever' in the middle of code when you want to use a new variable whilst coding, without having to scroll back to the start of the subprogram and insert it there with all the other DIM statements. A facility like this in Fortran would be nice.

0 Kudos
Mark_Lewy
Valued Contributor I
2,771 Views
Quoting - anthonyrichards
While we are mentioning Basic, Ilike the way thatVisual Basic allows you to add 'Dim variable As whatever' in the middle of code when you want to use a new variable whilst coding, without having to scroll back to the start of the subprogram and insert it there with all the other DIM statements. A facility like this in Fortran would be nice.


Perhaps the Fortran 2008 BLOCK construct is what you are looking for?

block_name: block
! declarations
! executable statements
end block block_name

0 Kudos
Reply