- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page