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

Typechecking when assigning string to integer

Karanta__Antti
New Contributor I
1,005 Views

My colleague bumped into a baffling case of which I made a short reproduction.

[fortran]program typechecktest

  integer :: someint

  someint = ' '

  print '(I10)', someint

end program
[/fortran]
The compiler is completely happy with assigning a string to an integer and gives no warnings.

Next I made the string a bit longer, e.g. 'hello'. Now the compiler says:

typechecktest.f90(6): warning #6043: This Hollerith or character constant is too long and cannot be used in the current numeric context. ['hello']
someint = 'hello'
------------^

Looking at the output integer in hex it turns out that the bytes in the integer are assigned the given characters.

What's going on here? Is it legal to assign a string to a variable of type integer? Is there some compiler option that I could use to make the compiler raise an error in these cases?

0 Kudos
2 Replies
Arjen_Markus
Honored Contributor II
1,005 Views
This is not standard Fortran, but an extension of the Intel Fortran compiler, going back a long time, IIRC.

The reason for the warning is that the string is interpret as a Hollerith constant. As the space where it
is to be stored is only 4 bytes long, the five characters are one character too much.

I suspect that explicitly specifyinga standard like Fortran 90will help detect this sort of things.

Regards,

Arjen
0 Kudos
Steven_L_Intel1
Employee
1,005 Views
Exactly right. I touched on this in a recent blog post.
0 Kudos
Reply