0

Trying to learn Fortran for a project. In a very simple program I am getting invalid character error.

program foo
   implicit none    
   integer :: n_samp
   integer :: samp_len
   integer :: x_len
   integer :: y_len
   n_samp=2
   samp_len=2
   y_len=11
   x_len=2
   real(8),dimension(n_samp,samp_len,y_len,x_len)=Yvec
end program foo

error generated by GFORTRAN

t.f90:11.12:

real(8), dimension(n_samp,samp_len,y_len,x_len)=Yvec
       1

Error: Invalid character in name at (1)

What is the cause of this error?

Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
pauli
  • 4,191
  • 2
  • 25
  • 41

2 Answers2

2

The correct syntax is

real(8), dimension(n_samp,samp_len,y_len,x_len) :: Yvec

The :: is obligatory when specifying any attributes (as the dimension in your case).

As @AlexanderVoigt points out, all variable declaration must be placed in the declaration part of the code, i.e., at the beginning.

I do not recommend using real(8) because that is not well defined, the 8 can mean anything, it is an index to a table of kinds and different compilers can have something different at place 8 in that table. See Fortran 90 kind parameter

Community
  • 1
  • 1
2

That's simple: You are not allowed to have declarations in the main body (that is after some instructions)! Instead, you should use parameters:

program foo
   implicit none    
   integer,parameter :: n_samp=2
   integer,parameter :: samp_len=2
   integer,parameter :: x_len=11
   integer,parameter :: y_len=2
   real(8),dimension(n_samp,samp_len,y_len,x_len) :: Yvec ! Add. typo here
end program foo
Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
  • @ alexander in my actual code i'm using the declarations as u have defined. Thanks for the input. – pauli Apr 28 '15 at 09:04