Newcomer and a few questions

Hello to all!

I am a newcomer in Fortran. My background is a bit of Pascal (back in Turbo Pascal days in the early '90s, as a University student), I have a good command of Mathematica (symbolic Mathematics exclusively) and I have a few failed attempts to learn languages like C (many corner cases and too cryptic and complex for my dyslexia) and Nim (quite overwhelming in features).
So I decided to give Fortran a try -as a hobby!

I have two questions:

  1. Do you have any learning resources (I would prefer books, for I am old school…)?
    For a start I have “Introduction to programming” by Ian Chivers and Jane Sleightholme.
    I am open to any suggestions (including other books and, perhaps, online tutorials)
  2. and a naive question

I tried this:

program help
    implicit none
    print *, "Hello World!"
end program

save as example.f90, then upon compiling (I have Void Linux @64bit and GNU Fortran)

$ gfortran example.f90 -o example

I get:

$ ./example
  Hello World!

Is there any explanation for the space before the Hello World?

Thank you all in advance!

1 Like

Welcome GeOdys in the Fortran Discourse!

The community has build this site with online tutorials and a list of good books:

2 Likes

That’s a (mis)feature of list-directed output —i.e., once upon a time, that first character had a special purpose.

If you provide an explicit format, then the leading space goes away:

program help
    implicit none
    print '(a)', "Hello World!"
end program
2 Likes

Naive questions are the more terrible…

Indeed it is an historical remnant related to old material… (other will explain it more precisely).

You can write to avoid that behaviour:

print '(A)', "Hello World!"

The '(A)' instead of the star is a format which says that a character string (symbolized by A) will follow.

This one could be a good start:

Don’t be impressed by the subtitle or the Chapter 1 (which exposes what you will know at the end of the book). The Chapter 2 will take you by the hand, starting from the Hello world example. The book is very progressively rising.

Thank you both very much for your replies!

1 Like

An approach complementary to print is to use write, e.g.

program help
   implicit none
   write (*, "(A)") "Hello World!"
end program

Arguably, this one is is easier to then adjust the destination of the output (the unit, here * symbolizes the default [typically the command line], though a different entry can address for instance an open file). And second, to define the format of the output, especially if the output consists of multiple (instances of) data types; e.g. (3(F8.5, 1X) I3.3) reports about three reals between to -9.99999 and 99.99999 separated each by a single space, followed by an integer up to 999 padded with leading zeroes (like 018) if the later is less than 100.

In the Olden Days of computing, there were big, noisy, machines called ‘line printers’. You’d position a big box of fan-folded paper under them, and an electro-mechanical ‘print train’ would hammer out characters on each line. Yes, they were very noisy. In fact, in some cases one often had to wear hearing protection around them.

Column 1 was used for vertical ‘carriage control’. You could put a ‘1’ in column 1 to skip to the next page, a ‘+’ to overprint a line, and a ‘0’ to double space. These were actually specified in the various Fortran Standards, up through Fortran 95. Since all of this has been obsolete since the 1980s, it simply became an annoyance. In Fortran 2003 they were deleted from the Standard.

3 Likes

@GeOdys, welcome to the forum!

Yes:

The LFortran compiler does not print it, unless you ask for it:

$ cat a.f90
program help
print *, "Hello World!"
end program
$ lfortran a.f90
Hello World!
$ lfortran --print-leading-space a.f90
 Hello World!
2 Likes

If you add the line

character(len=*),parameter :: list='(*(g0,1x))'

then

print list, x,y,a

will not have several behaviors that vary between compilers that list-directed I/O has(that is, I/O where the format is an asterisk). These include the amount of white-space between items, and at what line length line-breaks are added. It also gets rid of the leading space on the line. It does not print complex values as nicely as list-directed I/O does. There have been multiple discussions about it being time for the ASA carriage control legacy to be removed, the leading space being the primary remnant; but some platforms still support the ASA control and it would affect a lot of existing code (but if you are using list-directed output you left a lot of things up to the compilers that are at least as big as a leading space, so that is not a water-tight argument against dropping it).

But in general the asterisk is a convenience for constructing applications quickly; and production codes should use formatted output or risk unexpected text alignment or line breaks as well.

Some like to create a little module of favorite .formats.

csv='(*(g0:,","))'
packed='(*(g0))''
list='(*(g0,1x))'

I do not remember seeing an argument of any substance saying why the standard cannot
include saying when no format is present that the space prefix is not generated, as in

print, x, y, z, i, ...

that has been suggested multiple times and ignored just as many times! Also see

asa2pdf in Fortran Wiki

and the extensions

https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2023-0/open-carriagecontrol-specifier.html#GUID-5BA6BE56-7D84-468A-B5D7-C45B5AF05101

and related commands on some systems like asa and nasa.

PS:

Matching print is also an option. Just add “1x” at the beginning of all your Formats. A related question that comes up is why do so many Fortran libraries, particularly older ones
always use FORMAT statements that skip the first column of output? That is so they would work with the old ASA line printers. But sometimes new code does that as well; often just to be the same as the list-directed print statement!

2 Likes