FAQ in Fortran Wiki
# Frequently Asked Questions About Fortran ## Contents * [Which Fortran compiler should I use?](#compilers) * [What are good books on Fortran?](#books) * [How should one capitalize "Fortran?"](#capitalization) * [How do I produce a library?](#libraries) * [Why doesn't `u == 1.4D0` produce the expected result?](#fp-equality) * [How do I read until the end of a file (EOF)?](#eof) * [How do I read a character without having the user press enter?](#getkey) * [How do I set the precision of real variables in Fortran 90?](#real-precision) * [How do I convert a numeric variable to a string, or vice-versa](#strnum)? * [How do I read command-line arguments?](#cmdline) * [How can I create a temporary file?](#tmp) * [Can I allocate a variable length character string?](#allocstr) * [Which file extension should I use for my Fortran code?](#fileext) * [How do I initialize an array in row-column order?](#array_constructor) * [What is the equivalent of a short-circuiting compound conditional expression?](#short-circuit) * [Generating sequentially numbered output files](#numbered_files) * [How to write to stderr](#stderr) * [confusion about initializations and assignments](#init) ## desired ... feel free to answer * [How do I install Fortran on windows](#install) * [Which IDE do I use for windows](#ms_ide) * [Which IDE makes it easy and installs both compiler and everything](#ide) * [What is Fortran used for ?](#uses) * [How do I make a gui in Fortran](#gui) * [How do I use fpm](#fpm) * [How do I use coarrays](#coarrays) ----- ## Which Fortran compiler should I use? {: #compilers } [[GFortran]], [[G95]], [[Open Watcom]], and Silverfrost are free Fortran compilers, while Absoft, IBM, [[Intel Fortran compiler|Intel]], [[LF Fortran|Lahey]], [[NAG Fortran compiler|NAG]], Pathscale, PGI, and [[Oracle Solaris Studio|Oracle]] produce commercial Fortran compilers. Polyhedron Software provides compiler comparisons at <https://polyhedron.com/?page_id=175>. See [[Compilers]] for more information. ----- ## What are good books on Fortran? {: #books } See the [[Books]] page. ----- ## How should one capitalize "Fortran?" {: #capitalization } Standard capitalization is now the preferred way to write Fortran for several reasons, most notably because that is how recent versions of the [[Standards|standard]] write it. Another reason is due to an effort to standardize the capitalization of the names of programming languages. To quote Walt Brainerd (originally from the 1997年01月03日 version of Keith Bierman's Fortran FAQ): > The rule: if you say the letters, it is all caps (APL); > if you pronounce it as a word, it is not (Cobol, Fortran, > Ada). Some choose to write FORTRAN when referring to older versions of the language (prior to Fortran 90) to distinguish them from newer versions. ----- ## How do I produce a library? {: #libraries } To build a static library `libfoo.a` containing all modules and procedures in the `.f90` files in the current directory on Linux: % gfortran -c *.f90 % ar cr libfoo.a *.o The first command builds the object files and the second archives the object files into a static archive. To build a shared library `libfoo.so`: % gfortran -shared *.f90 -o libfoo.so -fPIC In both cases, other compiler flags such as `-O2` can be used. ----- ## Why doesn't `u == 1.4D0` produce the expected result? {: #fp-equality } This has to do with the representation of real values as (binary) floating point values. See [[Floating point arithmetic]]. ----- ## How do I read until the end of a file (EOF)? {: #eof } A common [[Fortran 95]] idiom for reading lines until the end of file is integer :: stat character(len=100) :: buf open(15, file='foo.txt') do read(fh, iostat=stat) buf if (stat /= 0) exit ! process buf end do close(15) {: lang=fortran } This example catches all conditions, not just the end of file. To specifically catch the EOF in [[Fortran 2003]] one can use the [[iso_fortran_env]] module and replace the `if` condition above with if (stat == iostat_end) exit {: lang=fortran } See also: [EOF idiom?](http://groups.google.com/group/comp.lang.fortran/browse_thread/thread/149d94a68d30fba3) on [[comp.lang.fortran]]. ----- ## How do I read a character without having the user press enter? {: #getkey } There isn't a portable way to do this either in Fortran or C, in short, because the terminal controls when the input is sent to your program and by default it is buffered. You must request that the terminal send each key and the method for doing so is platform-dependent. [Clive Page's Fortran Resources](http://www.star.le.ac.uk/~cgp/fortran.html) has a section on "Reading single keystrokes from Fortran" which provides a couple of short C functions ([sys_keyin.c](http://www.star.le.ac.uk/~cgp/sys_keyin.c)) which can be called from Fortran to achieve the desired behavior on most Unix systems. John Urban also provides a [getkey](http://www.urbanjost.altervista.org/LIBRARY/libGPF/download/tmp/html/download.html) function, written in C and callable from Fortran. See also: [Get Key Function?](http://groups.google.com/groups?threadm=edf0a73b-ff05-4849-90f6-2aa93445d388@v35g2000pro.googlegroups.com) on [[comp.lang.fortran]]. ----- ## How do I set the precision of real variables in Fortran 90? {: #real-precision } See [[Real precision]]. ----- ## How do I convert a numeric variable to a string, or vice-versa? {: #strnum } There is no intrinsic procedure for converting character strings to numerical values, or vice-versa. However, this can be accomplished using internal file IO. To obtain a string representation of a numeric variable, one can perform a formatted write to a string, just as one does to a file. Similarly, a formatted read from a string can extract a numeric value. See the [[strnum]] program for an example. ----- ## How do I read command-line arguments? {: #cmdline } See [[Command-line arguments]]. ----- ## How can I create a temporary file? {: #tmp } open(7, form='unformatted', status='scratch') {: lang=fortran } This will create a temporary file that only lives until it is closed. It doesn't need a filename as it will not be permanently saved to disk (although it could be stored somewhere as a temporary file). In this example, the unit number is 7. The file will be deleted when the program terminates (but may not be deleted if the program terminates abnormally, i.e. crashes). Note that it is not permitted to prevent deletion of a scratch file by closing the file using a close statement with `status='keep'`. For more details, see [scratch files](https://fortranwiki.org/fortran/show/Scratch+Files). ----- ## Can I allocate a variable length character string? {: #allocstr } Yes, in [[Fortran 2003]]. Declare the variable and allocate with a given length as follows: ~~~~~~~~~~~~~~~~~~ {: lang=fortran } character(LEN=:), allocatable :: str integer :: n n = 27 allocate(character(LEN=n) :: str) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You will be well rewarded by reading [Character-handling in Fortran]( https://fortranwiki.org/fortran/files/character_handling_in_Fortran.html) A [[Fortran 95]] solution is the [[iso_varying_string]] module, or the variable length string module in [[FLIBS]]. ----- ## Which file extension should I use for my Fortran code? {: #fileext } Although there are no official file extensions for Fortran code, there are two widely established conventions. Some use `.f` for fixed-form source and `.f90` for [[Free form layout]]. The latter is a reference to the [[Fortran 90]] standard, when [[Free form layout]] was introduced. The code contained in the file can be [[Fortran 95]], [[Fortran 2003]], etc. Others prefer to use file extensions that indicate the standard under which the code was written. For example, `.f03` for [[Fortran 2003]] code and `.f08` for [[Fortran 2008]] code. Unfortunately, this results in a proliferation of file extensions and some compilers may not support the newer extensions yet. See [[File extensions]] for more discussion on these issues. ----- ## How do I initialize an array in row-column order?{: #array_constructor } See [[array constructors]] ------ ## How to write to standard error (AKA "stderr"){: #stderr} See [[stderr]] ------ ## the equivalent of a short-circuiting compound conditional expression? {#short-circuit} Fortran does not require short-circuiting compound conditional expressions. New short-circuiting expressions are expected to be supported in Fortran 202x; but how do you produce an equivalent in Fortran in general? See [[short-circuiting ]]. ----- ## Generating sequentially numbered output files{#numbered_files} **Q**: How do I create numbered file names such as out_1.txt, out_2.txt, etc.? **A**: Use an "internal write" to create the file names, which is when you write into CHARACTER variables instead of files. for example character(len=4096) :: file_name write (file_name,"('out_',i0,'.txt')") i The string prefix and suffix may be variables as well: write (file_name,"(a,i0,a)") 'out_',i,'.txt' A format descriptor such as "I0.4" could be used instead of "I0" to add leading zeros to the numeric labels up to the specified number of digits. An extended example that generates sequentially numbered files and then deletes them is at [[sequential_filenames]]. For a related function see [[File_Name_Generator]]. ----- ## Confusion about initialization and assignment{: #init} A variable declaration can only initialize a variable, not assign a value on each procedure call. This and other issues are described in [[init]]. ----- ## External Links * [The Fortran FAQ](http://www.faqs.org/faqs/fortran-faq/) * [Fortran FAQ Wikibook](http://en.wikibooks.org/wiki/Fortran/FAQ) * [FORTRAN in Wikipedia](http://en.wikipedia.org/wiki/Fortran)
AltStyle
によって変換されたページ
(->オリジナル)
/
アドレス:
モード:
デフォルト
音声ブラウザ
ルビ付き
配色反転
文字拡大
モバイル