- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Link Copied
6 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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."
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page