PROGRAM hdf5f2003
  ! Required for type(C_PTR) (requires F2003)
  USE ISO_C_BINDING

  ! Include hdf5 module
  USE hdf5
  
  IMPLICIT NONE

  ! IDs for file, dataspace and dataset
  INTEGER(HID_T)            :: h5id, dspaceid, dsetid
  INTEGER                   :: h5error

  ! Long integer to be saved (64-bit)
  INTEGER(kind=8), TARGET   :: val

  ! Only scalar
  INTEGER(HSIZE_T), DIMENSION(1)    :: dims = (/1/)
  INTEGER                           :: rank = 1

  ! Pointer to value (requires F2003)
  TYPE(C_PTR) :: f_ptr

  ! Init value to be saved
  val = HUGE(val)
  WRITE (*,*) "Value: ", val

  ! Open Fortran HDF5 interface
  CALL h5open_f(h5error)

  ! Create new HDF5 file
  CALL h5fcreate_f('longint.h5', H5F_ACC_TRUNC_F, h5id, h5error)

  ! Create dataspace and dataset for long integer (64-bit)
  CALL h5screate_simple_f(rank, dims, dspaceid, h5error)
  CALL h5dcreate_f(h5id, 'Testvalue', H5T_STD_I64LE, dspaceid, dsetid, h5error)

  ! Get c-pointer to value to be saved
  f_ptr = C_LOC(val)

  ! Write value to HDF5 dataset
  CALL h5dwrite_f(dsetid, H5T_STD_I64LE, f_ptr, h5error)

  ! Close dataset
  CALL h5dclose_f(dsetid, h5error)

  ! Close dataspace
  CALL h5sclose_f(dspaceid, h5error)

  ! Close HDF5 file
  CALL h5fclose_f(h5id, h5error)

  ! Close Fortran HDF5 interface
  CALL h5close_f(h5error)
  
END PROGRAM hdf5f2003
