It is very simple query but I'm not able to find the exact solution. How to break to new line when printing in Fortran?
for example
print*,'This is first line'
print*,'This is second line'
I want following output
This is first line
This is Second line
That is add space between two lines.
In java we use \n
and in html using <br>
does the job..but how to achieve same in Fortran?
In Fortran, a statement must start on a new line. If a statement is too long to fit on a line, it can be continued with the following methods: If a line is ended with an ampersand, &, it will be continued on the next line. Continuation is normally to the first character of the next non-comment line.
Fortran Continuation Lines If a line is ended with an ampersand, &, it will be continued on the next line. Continuation is normally to the first character of the next non-comment line. A = 174.5 * Year & + Count / 100
Intel provides the option to disable the automatic linebreak. Refer to "Intel® Fortran Compiler 17.0 Developer Guide and Reference", you will find the option Add it to compiler flags. Continue your normal work flow. And find some time to update your code later.
There are several ways to print two lines of output. is one way to achieve what you want. Another is to do Show activity on this post. There are a number of ways to manage what you want. We can either print blank records or explicitly add a newline character. NEW_LINE ('a') is likely to have an effect like ACHAR (10) or CHAR (10,KIND ('a')).
There are several ways to print two lines of output.
program foo
print *, 'This is the first line'
print *, 'This is the second line'
end program
is one way to achieve what you want. Another is to do
program foo
write(*,'(A,/,A)') 'This is the first line', 'This is the second line'
end program foo
And, yet another way
program foo
write(*,'(A)') 'A' // achar(13) // achar(10) // 'B'
end program foo
And with some compilers you can use options
program foo
write(*,'(A)') 'A\r\nB'
end program foo
Compiling with the following options yields:
$ gfortran -o z -fbackslash a.f90 && ./z
A
B
There are a number of ways to manage what you want. We can either print blank records or explicitly add a newline character.
A newline character is returned by the intrinsic function NEW_LINE
:
print '(2A)', 'First line', NEW_LINE('a')
print '(A)', 'Second line'
NEW_LINE('a')
is likely to have an effect like ACHAR(10)
or CHAR(10,KIND('a'))
.
A blank record can be printed by having no output item:
print '(A)', 'First line'
print '(A)'
print '(A)', 'Second line'
Or we can use slash editing:
print '(A,/)', 'First line'
print '(A)', 'Second line'
If we aren't using multiple print statements we can even combine the writing using these same ideas. Such as:
print '(A,:/)', 'First line', 'Second line'
print '(*(A))', 'First line', NEW_LINE('a'), NEW_LINE('a'), 'Second line'
NEW_LINE('a')
could also be used in the format string but this doesn't seem to add much value beyond slash editing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With