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

How to use write command to put data into a character* (similar to sprintf in C)

acobulareddy
Beginner
993 Views

The following program is working fine in UNIX where as printing ****** in windows. Please let me know what is wrong with write(STRING1,I9) 12.35 statement

program fortrantest

CHARACTER*9 CLOCK1

write (CLOCK1,'(I9)')12.35

write(*,*)"REDDYO - ",CLOCK1

end program fortrantest

Thanks

Chandra.

0 Kudos
4 Replies
jimdempseyatthecove
Honored Contributor III
993 Views
Quoting - acobulareddy

The following program is working fine in UNIX where as printing ****** in windows. Please let me know what is wrong with write(STRING1,I9) 12.35 statement

program fortrantest

CHARACTER*9 CLOCK1

write (CLOCK1,'(I9)')12.35

write(*,*)"REDDYO - ",CLOCK1

end program fortrantest

Thanks

Chandra.


You have an argument mis-match between your format string (I9) = integer width of 9, and 12.35 which is either real(4) or real(8). Make a change to make the two consistent (both integer or both float).

Jim Dempsey

0 Kudos
Luis2
Beginner
993 Views

program main
CHARACTER*9 CLOCK1
real x
x=12.35
write (CLOCK1,'(f5.2)') x
write(*,*) "REDDYO - ", CLOCK1
end program

0 Kudos
jparsly
New Contributor I
993 Views

Others have pointed out that there is a mismatch between your data and your format. Since this is an error, compilers are free torespond however they like. I've seen three distinct behaviors from various compilers for this particular error:

1. Crap out with error message

2.Truncate the real to an integer, print the integer, and keep going

3. Act as if the real variable was equivalenced to an integer, and attempt to print the integer version of what is in the memory location. This is likely to get you *****'s because the integer version is often a very large number. I suspect in these cases this is an "argument association" type of equivalence where a call to the run-time library for formatted output is passing the real variable's address to a routine that is expecting an integer.

0 Kudos
Steven_L_Intel1
Employee
993 Views

Intel Fortran does #3 by default. To have it give an error message, add the option /check:fornat

0 Kudos
Reply