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

The confusing world of carriage control

dboggs
New Contributor I
763 Views
Since I like to have my console messages lined up, and indented by various amounts (0, 2, or 4 columns, etc.), I have been driven nuts by the apparent inconsistency of being off-by-one, due to Fortran's curious ("helpful") carriage control features. Especially with IVF! (I'm using 12.0 now).

Documentation is not very helpful, so with some trial and error I believe I now have a handle on it and wish to share my findings with others. The following "reverse engineering" is likely incomplete, but hopefully not incorrect, and I hope others can confirm/add to the results.

In conventional Fortran, formattedoutput to a printer or console screen will use "Fortran Carriage Control" (FCC). The first character ofeach record is "stolen" for carriage control and does not appear. HOWEVER if you use list-directed formatting (e.g. PRINT * or WRITE (*, *) then Fortran inserts a leading blank for you, which is then stolen for carriage control, and you get your output on the next line beginning in column 1.

But in IVF this convention is not followed by default. It is controlled by the specifier CARRIAGECONTROL in the OPEN statement. If you are printing to a child window or to a file opened with this specification set to 'FORTRAN', then the behavior is FCC as described above. If you do not use this specifier (and who does!) then there is no FCC.

This would be fine, EXCEPT that list-directed output still has the leading blank inserted! In this case you don't want it! It will cause your output to begin in column 2 instead of column 1. There is no way to get your list-directed output to begin in column 1 short of specifying CARRIAGECONTROL='FORTRAN' in the units OPEN statment. This however will screw up all output using an explicit format statement.

Moral: don't expect consistency between explict and list-directed output according to traditional fortran rules, unless you consciously specify it. By default, if you want your output to begin in column 5 (eg), then specify 4 leading blanks when using explicit FORMAT but 3 leading blanks when using list-directed format. For output to a unit opened using FCC, then specify 5 leading blanks when using explicit FORMAT but 4 when using list-directed format.

In a console application, one normally doesn't OPEN the output unit at all. So don't expect FCC, and don't expect list-directed output to ever begin in column 1.

I can't findthis is documented anywhere.
0 Kudos
3 Replies
IanH
Honored Contributor II
763 Views
Carriage control went the way of the dodo in Fortran 90, though the leading blank in list directed output (required by the standard - see the last paragraph in 10.9.2 of F2008, documented under "Rules for List-Directed Sequential WRITE Statements" in the IVF docs) is probably a relic of it.

The docs for CARRIAGECONTROL do mention the defaults. Note the /ccdefault compiler option (and others) can influence things as well - this probably addresses your issue with the output unit.

Isn't the real moral that if you want to have things appear in specific columns then you need to use an explicit format? Use list directed formatting only when you don't care.
0 Kudos
mecej4
Honored Contributor III
763 Views
It is not carriage control that is the source of your confusion, but the (loose) rules of list-directed output. Apart from a few requirements that constrain it, list-directed output is implementation-dependent. It has been highlighted in several places at several times that, if one wants repeatability and full control over output format, one should not use list-directed output.
0 Kudos
dboggs
New Contributor I
763 Views
Well, I have just learned that carriage control was "deleted" as of Fortran 03. Fine with me, in fact I would say good riddance. I had been studying Fortran 90 texts and back then it was still around.

However, I wish the committee had done a better job of deleting it. It was incomplete. The leading blank insertion for list-directed output should have been deleted too. Why would one possibly want it unless they were using carriage control?

So there are two things that cause confusion: (1) the incomplete deletion, and (2) lack of documentation re default behavior (if you say it's there OK but I can't find it; there is lots of info about the CARRIAGECONTROL identifier used in the OPEN statement but nothing describing a default).

As far as list-directed output itself goes, I find it very convenient and will continue using it much of the time. Now I know to subtract 1 from the left indent that I want. I would disagree with those who say it's only useful if you don't care about formatting. It's great use is that, if you primarily want to know the real number that the system really is using, you can automatically get an appropriate format without having to worry about how many digits to specify, how much spaceto allow for, etc.

Maybe I'm not the only one that was confused and this thread will be useful to others.
0 Kudos
Reply