Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Not sure if my compiler can handle format statements 1

Status
Not open for further replies.
Jul 17, 2014
4
US
Okay, so Im working through some basic stuff with learning fortran, but Im running up against an issue with format statements.

Using gfortran 4.7.2.1, I try to compile this example program from a tutorial pdf Im reading:

Code:
      PROGRAM FORMAT
      implicit none
      integer:: i, j
      real:: x
      character(len=10):: mychars
      write(*,*)' Enter digits 0 to 9 twice in succession '
      100 FORMAT(1x, a5, i5, f6.2, i3)      
      read(*,100)mychars, i, x, j
      write(*,*)mychars, i, j, x
      stop
      end program FORMAT

when I try to compile, I get

gfortran said:
bruce@bruce:~/development/fortran$ gfortran -o format format.f
format.f:11.7:

100 format(1x, a5, i5, f6.2, i3)
1
Error: Invalid character in name at (1)
format.f:10.16:

read(*,100)mychars, i, x, j
1
Error: FORMAT label 100 at (1) not defined
bruce@bruce:~/development/fortran$

The issue only pops up when trying to use format statements. Are format statements not supported by gfortran, or am I writing them in an outdated syntax version?
 
If your file extension is .f or .for, then it assumes that you are programming in F77 i.e. fixed format. Columns 1-5 for labels (your 100 you be in here), Column 6 for continuation and Column 7-72 for code.
 
Please change thread title to "I am not sure if I can handle format statements" [bigsmile]

Yes, Fortran 77 is a bit weird in that some blank spaces are significant (firs 6 columns).

Yes, Fortran 90 is a bit weird in that all blank spaces (outside quoted text) are ignored.

Anyway...

A few pointers:
[ul]
[li]You should make sure that you are learning Fortran 90 or later.[/li]
[li]Give your Fortran source files an extension of ".f90"[/li]
[li]Please do not learn from very old code where all text is left justified.[/li]
[li]Please do not use ALL UPPERCASE like they used to (great...you are already doing that).[/li]
[li]Practice good indentation (4 blank spaces per level).[/li]
[li]Do not use hard tabs, use emulated tabs...make sure your editor replaces tabs with blank spaces, look into settings.[/li]
[li]Stop using numeric labels altogether to refer to FORMATs and use strings instead.[/li]
[/ul]
I see what you are practicing but, personally, I steer away from column-oriented reading; instead, you should stick to list-oriented read. Use format only for writing. Here is your program, for example, no using labels.

Code:
program format
    implicit none
    integer:: i, j
    real:: x
    character(len=10):: mychars
    character(len=80):: fmt
    
    fmt = '(1x, a5, i5, f6.2, i3)'
    write(*,*)' Enter digits 0 to 9 twice in succession '
    read(*,fmt) mychars, i, x, j
    write(*,*)mychars, i, j, x
    stop
end program format

Keep it up, Fortran is nice, easy to program and easy to read (no unnecessary ornaments and the like). Make sure you have an editor with syntax highlighting and can handle Fortran 90 and beyond (Notepad++, for example).


 
I cant seem to find where I can change the thread title. I will change that if I can find out how to change it

Changing the extension to .f90 fixed the problem, thank you.

I am learning Fortran 90, my main resource being this pdf

Link

(which I have found to be quite easy to understand, for what its worth)

no worries about the uppercase thing, that was just the way the example was written. I was trying the FORMAT keyword in uppercase to see if that would fix the problem. I will refrain from talking in capital letters in the future ;)

I will try to indent a bit more clearly, but I was having problems with lines extending too many spaces. I suspect that should be fixed now that gfortran knows it should be compiling fortran 90 instead of fortran 77

I tried that example program you posted and it works fine, but I dont understand how passing through a character string to the read statement does that.

Funny that you should mention Fortran as being easy. It does actually seem a lot more like a scripting language than C and C++ do. (C++ was the first language I learned)

Somewhat off topic, but is Fortran really used all that much anymore? I mostly decided to learn it just for the heck of it, but it doesnt seem to be all that common anymore.
 
I was kidding about the title change, of course...are you too?

Fortran is case-insensitive.

Fortran 77 was limited to 72 columns wide; I think Fortran 90 may by default allow up to 132, if not, a simple compiler flag should do.

What is there to not understand in passing a string to the 'format' statement? That is the same thing you were passing before...an explicit quoted string, but nevertheless a string...I just simply chose to pass a string variable, instead.

First...lingo...Fortran, C/C++ are not called scripting languages. Typically, it is understood that a scripting language is one that does not require explicit user compilation ahead of execution; what's more, with a scripting language, one can be in the corresponding shell and execute commands interactively...think DOS terminal in Windows, cshell/bash on Linux...think tcl, python, etc.

Other than that, sorry you had to learn C++ first ;-)

Fortran is easy enough to have been the language of choice for many non-computer-scientists, i.e., engineers and scientists. And there is a reason why, after 57 years, is still around. Also, so much code was developed with it and debugged over the years to such perfection, that nobody dares to rewrite it. Anyway, here is one recent article I run into not long ago.

Universities choice of first programming language to teach is not necessarily based on technical merit, often times is a matter of phad...switching away from Fortran to C, from C to C++, from C++ to Java and I hear they are now switching away from Java to Python...going back almost full circle to another language that is easy to learn, write and read.

Finally, to address each of your statements/questions, depending which field you go to...sure, you will find yourself with some Fortran code. Not in Business, though; but then you will need to watch out for another dinosaur, there...COBOL [bigsmile]




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top