Fortran runtime error: Missing initial left parenthesis in format

When I ran an executable, it mostly just said segmentation fault. But, occasionally I get:

At line 1400 of file step_2.f (unit = 6, file = 'stdout')
Fortran runtime error: Missing initial left parenthesis in format

This is what the WRITE and FORMAT statements look like:

gfortran -g test.f90 -Wall -Werror -fmax-errors=1 -fcheck=all -fPIC -std=legacy -o test -lgfortran

10 FORMAT(’ ',7A,$)

What’s wrong?

This code appears to be to clear the screen:


A small testcase works to clear the screen without the error:

gfortran -g test.f90 -Wall -Werror -fmax-errors=1 -fcheck=all -fPIC -std=legacy -o test -lgfortran

  program test
  write (*,*) 'Routine test start'
  write (*,10) CHAR (27),'[H',CHAR(27),'[2J'

10 FORMAT(‘’,8A,$)

  write (*,*) 'Routine test end'
  end program test

As-is neither of the two snippets will compile I think. Ensure that the single quote characters are the usual one ' and not some impostor look-alike character.

You probably want to avoid using the “$” as that is non-standard. The standard equivalent is to use non-advancing I/O. It is usually best to avoid using specific lengths like “7” as that can be prone to error and truncation. I might suggest something more like

 program testit
   call clear_screen()

subroutine clear_screen()
!@(#) clear_screen(3f): clear the screen of an ANSI(1) terminal using escape sequences
character(len=*),parameter :: CSI=char(27)//'['
character(len=*),parameter :: CLEAR=CSI//'2J'
character(len=*),parameter :: HOME=CSI//'H'
   write(*,'(a)',advance='no') CLEAR,HOME
end subroutine clear_screen

end program testit

The format string is stored in a writable segment and parsed at runtime when the write statement executes. It may have been overwritten during execution before being referenced, although with “fcheck=all” I am at a loss as to how. In my experience it is usually a subscript out of bounds error. I always go to valgrind when similarly baffled.

I have a similar problem. I do not see any error. I will post the code and error message here. I am sorry if I should be posting in a different way - I am new to this site program example
more example.f

        program example
        implicit none
        integer j
        character single_quote*1, fmt*6
        single_quote = ''''
        fmt = single_quote // '(I2)' // single_quote
        j = 42
1       format (i2, x, ':', a6, ':')
        write (*, fmt) j

% gfortran -fcheck=all -o example example.f
% example
42 :‘(I2)’:
At line 10 of file example.f (unit = 6, file = ‘stdout’)
Fortran runtime error: Missing initial left parenthesis in format

Error termination. Backtrace:
#0 0x10a4a886e

In the previous post there were asterisks in the write statements that disappeared when posting. The code reads: write(,1) and write(,fmt)

write(asterisk,1) write(asterisk,fmt)

@slord your fmt starts with a quote, but the string should start with (.

Replace the line

      fmt = single_quote // '(I2)' // single_quote


      fmt = '(I2)'

The first character of a format string should be ‘(’. The string, as stored, will have no quotes. In other circumstances, for instance, when the format string contains literal text as well as Format specfiers, you may need the format string to contain embedded quotes. For the simple case that you have, that is not needed.

In addition, a general tip is that unless you have a good reason ( there are some) you want to minimize the number of constant numbers used, as they tend to be error prone. Instead of using a string where you have to count the characters, or an integer field that might overflow, you can make your code more generic and less prone to errors with a few changes like this (but sometimes you do want very specific formats for nice columns or to not display insignificant digtits, etc ) …

program example
implicit none
! an unchanging format can be specified as a constant 
character(len=*),parameter :: fmt='(I2)'
integer                    :: j
   j = 42
   ! you can just use "a" to accomodate a string of any length
   ! you can use i0 for an indeterminant number of digits too
   ! if you want
1  format (i0, x, ':', a, ':')
   write (*, fmt) j

Thanks - for the solution, the quick reply, and the fine help resource!