Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28559 Discussions

Is type-spec needed for character array literals?

martymike
Novice
536 Views

I had understood that type-specs are needed for character array literals if the values are of different lengths. It appears that ifort reliably creates an array of the length of the longest element of the array when an array literal is initialized with strings of different lengths. For example:
character(2) :: a='12'
character(16) :: b='abcdefghijklmnop'
character(44) :: c='The quick brown fox jumps over the lazy dog.'

 

It appears that an array literal, say, [a,b,c], will always be length 44. Is this an intentional feature that can be relied on?

0 Kudos
1 Solution
Steve_Lionel
Honored Contributor III
492 Views

It's a deliberate extension, non-portable, of the Intel compiler. I'm surprised that you don't get a warning with /stand, however - that would be a bug. My advice would be to add the type-spec.

View solution in original post

0 Kudos
5 Replies
FortranFan
Honored Contributor II
523 Views

@martymike ,

It is a quality of compiler issue.

The language standard places the responsibility on the author of the code to have the same length-type parameter of the declared type in an array constructor e.g., with character type as you show in the original post or with a parameterized derived type with a LEN parameter.

A compiler can choose to rise above and detect and diagnose instructions that deviate from this e.g., in the "[a,b,c]" but the standard does not require this of a conforming processor.

If you have NAG Fortran compiler, you can try the code snippet below, chances are it will raise an error:

   character(len=2), parameter  :: a = '12'
   character(len=16), parameter :: b = 'abcdefghijklmnop'
   character(len=44), parameter :: c = 'The quick brown fox jumps over the lazy dog.'
   print *, [ a, b, c ]
end
0 Kudos
andrew_4619
Honored Contributor III
512 Views
You need standards checking on to get and error I think.
0 Kudos
martymike
Novice
507 Views

Tried /stand, no errors from that.

We are just trying to get a handle on what extensions to the Fortran standard we are relying on in our code.

I understood that the standard required the length to be either all the same or else specified, but was otherwise silent about the consequences, suggesting that would be implementation dependent. I think I remember an earlier implementation of IVF using the length of the first string for the length of the literal array.

This is such a nice way to handle the issue. I just wanted to be sure that it wasn't something that just happened to be working in a nice way, or if it was something Intel implemented intentionally.

0 Kudos
Steve_Lionel
Honored Contributor III
493 Views

It's a deliberate extension, non-portable, of the Intel compiler. I'm surprised that you don't get a warning with /stand, however - that would be a bug. My advice would be to add the type-spec.

0 Kudos
andrew_4619
Honored Contributor III
444 Views

warning #8208: If type specification is omitted, each ac-value expression in the array constructor of type CHARACTER must have the same length type parameters. ['12']

 

The code shown by the OP generates two warnings if standards checking is actually on. 

0 Kudos
Reply