Stop with non-zero exit status

Searching for this question I found this SO thread, and the suggestion is to use stop N. Is that a standard way to do it? Seems from the discussion that sometimes the number gets printed, or not, depending on the OS or the compiler.

What I want is a flag to allow easier handling of errors from the output of a Fortran program, using an upper level script.


The Fortran 2018 standard says:

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 QUIET= is omitted or the scalar-logical-expr has the value false:
• …
• if a stop code is specified, it is recommended that it be made available by formatted output to the same unit.

(ERROR_UNIT, I think)

Note there is also the ERROR STOP statement:

Execution of a STOP statement initiates normal termination of execution. Execution of an ERROR STOP statement initiates error termination of execution.


A process exit code has not always been a thing, and thus the Fortran standard cannot mandate anything about it. I will note that I have had fairly good luck with stop n passing n as the exit code on all platforms and compilers that I care about. I will note that detecting it on Windows is occasionally a bit finicky, I suspect more due to the idiosyncrasies of the cmd and/or batch shells than problems in the Fortran compilers or run-time systems.


When using stop 1 (for example), this becomes output on the program termination, like:

ERROR: Invalid input parameter.

Is it possible to suppress that printing?


is the Standard’s way


That is perfect, thank you!

By the way, that blog has many interesting short and recent articles in the Fortran category:

1 Like

I have also been using stop 1, that seemed to work everywhere. Recently I switched to error stop.

Regarding the quiet part, with GFortran 11.0.1 I get:

$ gfortran a.f90

    1 | STOP 1, QUIET=.TRUE.
      |       1
Error: Syntax error in STOP statement at (1)

So I don’t know if I am doing anything wrong.

It seems that it is feature not implemented yet?

1 Like

Looks like GFortran’s parser cannot parse it yet. In LFortran we can parse it:

$ cat b.f90 
program test
$ lfortran --show-ast b.f90 
(TranslationUnit [(Program test () [] [] [] [(Stop 0 1 (Logical .true.) ())] [])])

But the quiet=.true. gets lost by the time it gets to ASR:

$ lfortran --show-asr b.f90
(TranslationUnit (SymbolTable 1 {test: (Program (SymbolTable 2 {}) test [] [(Stop (ConstantInteger 1 (Integer 4 [])))])}) [])

And by the time it gets to LLVM, also the non-zero exit code gets lost:

$ lfortran --show-llvm b.f90
; ModuleID = 'LFortran'
source_filename = "LFortran"

@0 = private unnamed_addr constant [6 x i8] c"STOP\0A\00", align 1

define i32 @main() {
  call void (i8*, ...) @_lfortran_printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @0, i32 0, i32 0))
  call void @exit(i32 0)
  ret i32 0

declare void @_lfortran_printf(i8*, ...)

declare void @exit(i32)

We need to fix it: Implement nonzero exit code and `quiet` in STOP (#597) · Issues · lfortran / lfortran · GitLab

1 Like