15

This works just fine:

  program main
    integer,parameter,dimension(3) :: x = [1,2,3]
    print*,x
  end program main

As does this:

  program main
    integer,parameter,dimension(3) :: x = (/1,2,3/)
    print*,x
  end program main

Is there a reason to think that one form should be preferred over the other (e.g. backward compatibility)?

1 Answer 1

23

The square bracket form was added to the language in Fortran 2003. If you are writing to Fortran 90 (as per the tag on the question) then the square bracket form is a syntax error (square brackets are not in the Fortran 90 character set).

Beyond language standard it a question of personal preference and style.

Sign up to request clarification or add additional context in comments.

5 Comments

Do you know if there was any reason discussed as to why the language needed a second way to declare an array literal?
Not specifically. I suspect there was no pressing need, just a want, given the single character is more readable and their use is somewhat of a convention across languages.
That's kind of how I feel about it -- They looked at the language and said "Oops, we goofed up on this one the first time around ... Might as well make a second way to do it ...". sigh However, since I'm trying to target f90-f95, I suppose I'll stick with the ugly version. Thanks. +1 and checkmark to you.
Probably they wanted to avoid the occurance of too many subsequent parantheses: call somearrayfunc((/ 1, 2, 3 /)) versus call somearrayfunc([ 1, 2, 3]). For me at least, the 2nd form is more readable.
I believe this has been introduced with Coarrays in mind.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.