# The STOP statement and error\_unit

**URL:** <https://fortran-lang.discourse.group/t/the-stop-statement-and-error-unit/7102>\
**Category:** Uncategorized\
**Created:** [January 3, 2024, 10:08am UTC](https://fortran-lang.discourse.group/t/the-stop-statement-and-error-unit/7102 "2024-01-03T10:08:20Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [January 3, 2024, 10:08am UTC](https://fortran-lang.discourse.group/t/the-stop-statement-and-error-unit/7102/1 "2024-01-03T10:08:20Z")

</div>

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:

```auto
! 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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![DavidB](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@DavidB](https://fortran-lang.discourse.group/u/DavidB)\
**Post date:** [January 3, 2024, 10:29am UTC](https://fortran-lang.discourse.group/t/the-stop-statement-and-error-unit/7102/2 "2024-01-03T10:29:57Z")

</div>

> [@Arjen](#):
>
> I guess this is expected and standard behaviour

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.

Edit: say which version of standard.

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [January 3, 2024, 10:36am UTC](https://fortran-lang.discourse.group/t/the-stop-statement-and-error-unit/7102/3 "2024-01-03T10:36:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![davidpfister](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/davidpfister/32/3949_2.png) [@davidpfister](https://fortran-lang.discourse.group/u/davidpfister)\
**Post date:** [January 3, 2024, 12:31pm UTC](https://fortran-lang.discourse.group/t/the-stop-statement-and-error-unit/7102/4 "2024-01-03T12:31:09Z")

</div>

@Arjen, you might be interested by this article on [scivision](https://www.scivision.dev/fortran-stderr-stop-return-code/).  
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.

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [January 3, 2024, 12:39pm UTC](https://fortran-lang.discourse.group/t/the-stop-statement-and-error-unit/7102/5 "2024-01-03T12:39:25Z")

</div>

Interesting! Note that the compiler versions I used are not necessarily the latest ones (in fact for Linux I used Intel Fortran 2018 out of laziness).

---

<div class="post-metadata">

**Author:** ![DavidB](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@DavidB](https://fortran-lang.discourse.group/u/DavidB)\
**Post date:** [January 4, 2024, 12:27am UTC](https://fortran-lang.discourse.group/t/the-stop-statement-and-error-unit/7102/6 "2024-01-04T00:27:48Z")

</div>

> [@davidpfister](#):
>
> The Fortran 2008 and 2018 standards recommend that the error code be returned on `iso_fortran_env: error_unit`

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.

---

<div class="post-metadata">

**Author:** ![davidpfister](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/davidpfister/32/3949_2.png) [@davidpfister](https://fortran-lang.discourse.group/u/davidpfister)\
**Post date:** [January 4, 2024, 7:46am UTC](https://fortran-lang.discourse.group/t/the-stop-statement-and-error-unit/7102/7 "2024-01-04T07:46:24Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![DavidB](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@DavidB](https://fortran-lang.discourse.group/u/DavidB)\
**Post date:** [January 4, 2024, 10:27am UTC](https://fortran-lang.discourse.group/t/the-stop-statement-and-error-unit/7102/8 "2024-01-04T10:27:32Z")

</div>

> [@davidpfister](#):
>
> But standard or not that does not change the observations

Absolutely. I am trying to familiarize myself with the new standard. Looking stuff up helps.

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [January 4, 2024, 2:33pm UTC](https://fortran-lang.discourse.group/t/the-stop-statement-and-error-unit/7102/9 "2024-01-04T14:33:51Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [January 4, 2024, 2:49pm UTC](https://fortran-lang.discourse.group/t/the-stop-statement-and-error-unit/7102/10 "2024-01-04T14:49:55Z")

</div>

Thanks for this exegesis - so the behaviour I noticed is quite close to being standard, just an “epslion” away, you might conclude.

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [January 4, 2024, 3:52pm UTC](https://fortran-lang.discourse.group/t/the-stop-statement-and-error-unit/7102/11 "2024-01-04T15:52:51Z")

</div>

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.
