logical Boolean examples for C, C++, and Fortran

June 12, 2026 ยท View on GitHub

When interfacing between C or C++ and Fortran, the logical(C_BOOL) type is used to represent boolean values. This test demonstrates how to use logical(C_BOOL) in Fortran and how it interacts with C and C++ code.

The "test/bool/bad_interface.f90" and "src/bool/bad_bool.f90" files contain examples of incorrect usage of logical(C_BOOL), which should lead to runtime issues by messing up the values of following dummy arguments.

nvfortran supports F2018 standard C_BOOL if nvfortran -Munixlogical is used.

The logical constants .TRUE. and .FALSE. are defined to be the four-byte values -1 and 0 respectively. A logical expression is defined to be .TRUE. if its least significant bit is 1 and .FALSE. otherwise.

Example output, Fortran interfacing with C or C++

  • Intel oneAPI 2023..2026 (Linux or Windows) without -fpscomp logicals
  • NVHPC 2023.5 without -Munixlogical
 logical_not(T): F
   storage_size()  bits   hex(in)  hex(out)
         C_BOOL:     8         1        FE
 logical_not(true) should be false: 1

 logical_not(F): T
   storage_size()  bits   hex(in)  hex(out)
         C_BOOL:     8         0        FF
  • Intel oneAPI with -fpscomp logicals
  • NVHPC with -Munixlogical
 logical_not(T): F
   storage_size()  bits   hex(in)  hex(out)
         C_BOOL:     8         1         0

 logical_not(F): T
  storage_size()  bits   hex(in)  hex(out)
        C_BOOL:     8         0         1
 OK: boolean-logical not