Interop starts by choosing Fortran kinds that correspond to C-facing data widths. The example models the choice without calling C.

Program

Play the program to choose which integer kind label is selected.

kind_choice
kind_mapping.f90
Replay: real traced execution (multi-file project)
program kind_mapping_demo
    use iso_c_binding, only: c_int, c_long
    implicit none
    integer :: kind_choice
    integer :: chosen_kind
    character(len=12) :: label

    kind_choice = 1
    select case (kind_choice)
        case (1)
            chosen_kind = c_int
            label = 'c_int'
        case (2)
            chosen_kind = c_long
            label = 'c_long'
        case default
            chosen_kind = kind(0)
            label = 'default'
    end select
    print '(A, 1X, I0)', trim(label), chosen_kind
end program kind_mapping_demo
program kind_mapping_demo
    use iso_c_binding, only: c_int, c_long
    implicit none
    integer :: kind_choice
    integer :: chosen_kind
    character(len=12) :: label

    kind_choice = 2
    select case (kind_choice)
        case (1)
            chosen_kind = c_int
            label = 'c_int'
        case (2)
            chosen_kind = c_long
            label = 'c_long'
        case default
            chosen_kind = kind(0)
            label = 'default'
    end select
    print '(A, 1X, I0)', trim(label), chosen_kind
end program kind_mapping_demo
program kind_mapping_demo
    use iso_c_binding, only: c_int, c_long
    implicit none
    integer :: kind_choice
    integer :: chosen_kind
    character(len=12) :: label

    kind_choice = 3
    select case (kind_choice)
        case (1)
            chosen_kind = c_int
            label = 'c_int'
        case (2)
            chosen_kind = c_long
            label = 'c_long'
        case default
            chosen_kind = kind(0)
            label = 'default'
    end select
    print '(A, 1X, I0)', trim(label), chosen_kind
end program kind_mapping_demo
  1. kind_choice ← 1

    8kind_choice = 19select case (kind_choice)
    values this step1kind_choice
  2. matched ← case (1)

    8kind_choice = 19select case (kind_choice)10    case (1)
    values this stepcase (1)matched1kind_choice
  3. chosen_kind ← c_int

    10case (1)11    chosen_kind = c_int12    label = 'c_int'
    values this stepc_intchosen_kind
  4. label ← c_int

    11    chosen_kind = c_int12    label = 'c_int'13case (2)
    values this stepc_intlabel
  5. print '(A, 1X, I0)', trim(label), chosen_kind

    19    end select20    print '(A, 1X, I0)', trim(label), chosen_kind21end program kind_mapping_demo
    outputc_int 4
    values this stepc_intlabelc_intchosen_kind
  1. kind_choice ← 2

    8kind_choice = 29select case (kind_choice)
    values this step2kind_choice
  2. matched ← case (2)

    8kind_choice = 29select case (kind_choice)10    case (1)
    values this stepcase (2)matched2kind_choice
  3. chosen_kind ← c_long

    13case (2)14    chosen_kind = c_long15    label = 'c_long'
    values this stepc_longchosen_kind
  4. label ← c_long

    14    chosen_kind = c_long15    label = 'c_long'16case default
    values this stepc_longlabel
  5. print '(A, 1X, I0)', trim(label), chosen_kind

    19    end select20    print '(A, 1X, I0)', trim(label), chosen_kind21end program kind_mapping_demo
    outputc_long 8
    values this stepc_longlabelc_longchosen_kind
  1. kind_choice ← 3

    8kind_choice = 39select case (kind_choice)
    values this step3kind_choice
  2. matched ← case default

    8kind_choice = 39select case (kind_choice)10    case (1)
    values this stepcase defaultmatched3kind_choice
  3. chosen_kind ← default integer

    16case default17    chosen_kind = kind(0)18    label = 'default'
    values this stepdefault integerchosen_kind
  4. label ← default

    17        chosen_kind = kind(0)18        label = 'default'19end select
    values this stepdefaultlabel
  5. print '(A, 1X, I0)', trim(label), chosen_kind

    19    end select20    print '(A, 1X, I0)', trim(label), chosen_kind21end program kind_mapping_demo
    outputdefault 4
    values this stepdefaultlabeldefault integerchosen_kind
C kinds `c_int` and `c_long` name interoperable integer kinds.
selection `select case` can centralize the mapping decision.
label plus kind A trace can carry both the human label and the selected kind value.