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
write(*,*)'start'
call clear_screen()
write(*,*)'end'
contains
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
write(*,1)j,fmt
1 format (i2, x, ':', a6, ':')
write (*, fmt) j
end
% 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
‘(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
write(*,1)j,fmt
! 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
end