I've been searching for a considerably long time for this. Does anyone know how to clear the screen in a console app in Fortran language? any help will be very much appretiated!
7 Answers
Fortran, qua Fortran, knows nothing of such concepts as screens or keyboards or, for that matter, computers. There is, therefore, no language-standard way of clearing a screen from Fortran. You will have to find some platform-dependent approach.
Most Fortran compilers have some way of doing this, for example Intel Fortran provides the SYSTEM function.

- 77,191
- 7
- 105
- 161
-
3+1 It has been so long I can't remember ever clearing the terminal (or needing to clear it)... God I used to love FORTRAN! – MoonKnight Jun 06 '12 at 13:38
-
I used the windows batch script command for clearing the screen call system ("cls") – rMaero Jun 06 '12 at 17:29
Contrary to others I would not call SYSTEM()
(standard Fortran 2008 alternative is execute_command_line()
) but I would print right ANSI escape code http://en.wikipedia.org/wiki/ANSI_escape_code:
print *, achar(27)//"[2J"
This will be much faster than calling SYSTEM()
.
This works in typical Linux terminals, but will not work in the MS Windows terminal.
Another more practical reference how to use the escape code is http://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html

- 57,977
- 4
- 76
- 119
-
What's the Fortran 2003 standard alternative you refer to ? – High Performance Mark Jun 06 '12 at 18:05
-
http://gcc.gnu.org/onlinedocs/gfortran/EXECUTE_005fCOMMAND_005fLINE.html#EXECUTE_005fCOMMAND_005fLINE – Vladimir F Героям слава Jun 06 '12 at 18:06
-
As it says at the link, Fortran 2008 and later, not Fortran 2003. – High Performance Mark Jun 06 '12 at 18:07
-
You are right, I generally have problems distinguishing f2003 and f2008 as compilers do not implement the features in any order :( – Vladimir F Героям слава Jun 06 '12 at 18:08
-
@VladimirF - "... in any order", what do you mean? They implement them according to the standard they belong to, and user requests. They're not implemented chronologically, if that's what you ment. – Rook Jun 06 '12 at 18:40
-
Different vendors support different mix of features from f2003 and f2008. Some features from f2008 are implemented before other features from f2003, but the order is vendor specific. – Vladimir F Героям слава Jun 06 '12 at 21:32
In Fortran 90/95 your best option is the system command which is a vendor supplied extension (i.e., not part of the F90/95 standard so some obscure Fortran compilers may not have it but all major ones do).
$ cat clear.f90
program
call system('clear')
end
$ gfortran clear.f90 -o clear
$ ./clear

- 321
- 2
- 9
It depends on your specific sytem and compiler. There is no general way. Fortran doesn't know about specific hardware devices like terminal screens and printers. (Neither do most other languages). The details depend entirely on your specific system.
My advice would be to clear the terminal by invoking the relevent script via the command line - but this is not nice. it is generally more portable to write the output to an ordinary text file and then execute appropriate system commands to print that file to screen. This way you can manipulate the file as you wish...
See here for a simalar question from which these some of the above text was salvaged.

- 23,214
- 40
- 145
- 277
In Fortran ACHAR(N)
returns the ASCII of N, so my preferred method would be:
WRITE(*,'(2f15.9,A5)',advance='no') float1,float2,ACHAR(13)
ACHAR(13)
returns carriage return character \r
in Python. So after printing, it returns the cursor to the beginning of the line where it can be overwritten.
After you are out of the loop you can use CALL SYSTEM('clear')
to clean the screen.
This is helpful since CALL SYSTEM('clear')
is slower and uses a lot of CPU, you can check this by replacing the above method with
WRITE(*,'(2f15.9)',advance='no') float1,float2;CALL SYSTEM('clear')
and check the difference in time taken in loops.

- 732,580
- 175
- 1,330
- 1,459

- 829
- 5
- 18
-
This is more of a clear line than clear screen, though. Specifically the same the escape code 1 G would do, move cursor to column 1. – Vladimir F Героям слава Oct 22 '20 at 21:00
-
@VladimirF this ASCII 13 thing doesn't work in all terminals either, is there any alternative way? `print *, achar(27)//"[2J"` doesn't really seem to stay static on the page for me. – Shriraj Hegde Oct 27 '20 at 10:28
-
Depends on your exact purpose. My answer was really just for clearing the screen and nothing more. If you need to write something on a static place on the screen, ASCII 13 is not bad. The alternative is `print *, achar(27)//"[1G"` but it is pretty much the same as ASCII 13 and also only works on some terminals. – Vladimir F Героям слава Oct 27 '20 at 10:40
-
@VladimirF how about printing characters in the file clear.txt after `clear > clear.txt`? It is working for me. – Shriraj Hegde Nov 13 '20 at 05:14
I found yet another way to clear screen in UNIX like systems by printing the output of clear
command
(from manual of clear command which states you can write the output to a file and then cat
it to clear the screen)
So better way is to clear > temp.txt
and use the characters in files in a print statement
Although both do the same thing, calling SYSTEM("foo") is very very (100+ times) slower than directly printing those characters
For example:
program clear
implicit none
INTEGER::i
do i = 1, 1000
print *, "Test"
call system("clear")
enddo
end program clear
This program takes anywhere between 3.1 to 3.4 seconds.
But instead of calling system, if I directly print the characters like this :
program clear
implicit none
INTEGER::i
do i = 1, 1000
print *, "Test"
print *, "[H[2J[3J"
enddo
end program clear
This produces the exact same result but takes anywhere between 0.008 to 0.012 seconds (8 to 12 ms)
In fact, running the second loop 100,000 is faster than CALL SYSTEM("clear")
1000 times
EDIT: Don't copy-paste from here, it won't work, (characters are replaced on StackOverflow)
Just use clear > file
and copy the contents of the file into a print statement
Screenshot of actual code(from preview):
These characters aren't supported I guess, They disappear in the answers (They work fine in preview)

- 829
- 5
- 18
-
@VladimirF don't think so, the escape code in your answer behaves somewhat like 'ctrl+L' for me, like the screen gets cleared but cursor doesn't return,something like that. Like all written characters on terminal are replaced with blank space characters – Shriraj Hegde Nov 13 '20 at 10:41
-
There is 2J and 3J, they are quite similar: *If n is 2, clear entire screen (and moves cursor to upper left on DOS ANSI.SYS). If n is 3, clear entire screen and delete all lines saved in the scrollback buffer (this feature was added for xterm and is supported by other terminal applications).* https://en.wikipedia.org/wiki/ANSI_escape_code – Vladimir F Героям слава Nov 13 '20 at 10:44
-
@VladimirF IDK, even `print *,achar(27) // "[H[2J[3J"` isn't producing the exact result as `CALL SYSTEM("clear")` – Shriraj Hegde Nov 13 '20 at 10:55
-
Yes, I am trying to understand what exactly it actually does. Whether it is a Unix-terminal specific and why there is the `[2J` in common. – Vladimir F Героям слава Nov 13 '20 at 10:55
-
@VladimirF Check my edit, a character goes missing in the answer but works in the preview – Shriraj Hegde Nov 13 '20 at 10:59
-
Okay I have figured it out, the missing character is ASCII 27 `print *,achar(27),"[H",achar(27),"[2J",achar(27),"[3J"` This works as intended – Shriraj Hegde Nov 13 '20 at 11:17
-
That's what I supposed. This is exactly the ANSI/CSI sequence 2J and 3J I linked. – Vladimir F Героям слава Nov 13 '20 at 11:27
-
3J seems optional but [H seems important to not leave behind emply characters – Shriraj Hegde Nov 13 '20 at 11:32