I just ran into a curious phenomenon: the STOP statement with a text causes the program to write that text to the error_unit (standard error). This happens on Windows and Linux with both gfortran and Intel Fortran oneAPI. I guess this is expected and standard behaviour, but I ran into it because our testbench set-up assumes that writing to standard error means something went wrong.
I wrote the following miniscule program to test the behaviour:
! stop_exit.f90 --
! Does a stop statement with trext always mean that the text goes to standard error?
!
program stop_exit
implicit none
write(*,*) 'Program simply executes "stop"'
stop 'stop'
!stop
end program stop_exit
and ran it via:
stop_exit 1>aa 2>bb
so that the output to standard output and standard error is split. In all four cases the file “bb” is filled with text.
The 2023 standard just says (Section 11.4 STOP and ERROR STOP statements)
When an image is terminated by a STOP or ERROR STOP statement, its stop code, if any, is made available in a processor-dependent manner. If the stop-code is an integer, it is recommended that the value be used as the process exit status, if the processor supports that concept. If the stop-code in a STOP statement is of type character or does not appear, or if an end-program-stmt is executed, it is recommended that the value zero be supplied as the process exit status, if the processor supports that concept. If the stop-code in an ERROR STOP statement is of type character or does not appear, it is recommended that a processor-dependent nonzero value be supplied as the process exit status, if the processor supports that concept.
Hm, the standard is tacit about this then, Well, the solution for me was to use a write statement to print the message (“Normal end”) instead of a stop statement. I would have expected this behaviour from error stop, not from stop.
@Arjen, you might be interested by this article on scivision.
The following is a quote from the previous link. I did not check the standard myself.
The Fortran 2008 and 2018 standards recommend that the error code be returned on iso_fortran_env: error_unit , which was first defined in Fortran 2003 standard. Earlier standards don’t say where the error code should go. Through Fortran 2018, stop with integer code is still normal program termination.
I can’t find this wording in either of the F2018 or F2023 standards.
The closest is in 12.5.1 para 5 of F2023
This document identifies a processor-dependent external unit for the purpose of error reporting. This unit shall be preconnected for sequential formatted output. The processor may define this to be the same as the output unit identified by an asterisk. This unit is also identified by a unit number defined by the named constant ERROR_UNIT of the intrinsic module ISO_FORTRAN_ENV.
I can’t find any requirement for ERROR STOP to write to ERROR_UNIT. Note: I am not fluent in standardese.
Me neither . I did not check the standard though, I simply quoted the reference I found on scivision. I edited my previous post accordingly But standard or not that does not change the observations from @Arjen.
If QUIET= is omitted or the scalar-logical-expr has the value false:
if any exception (17) is signaling on that image, the processor shall issue a warning indicating which exceptions are signaling, and this warning shall be on the unit identified by the named constant ERROR_UNIT from the intrinsic module ISO_FORTRAN_ENV (16.10.2.9);
if a stop code is specified, it is recommended that it be made available by formatted output to the same unit.
That’s about as close as it can be to implying that a character stop code in a STOP statement should be output on ERROR_UNIT without saying it outright.
When writing fortran programs that are used in pipes (i.e. sequences of commands that all read from stdin and write to stdout and are used as: prog1 | prog2 | prog3), it is inconvenient for anything extra to be written to stdout. I long ago stopped using STOP statements in main programs for this reason. Many compilers would write “STOP” or “STOP 0” to stdout when encountering a stop statement. In the past, this was not covered in the standard, and compilers were free to add such output or not. It is a good thing that this is finally being standardized.