Search code examples
cmisra

Is it safe to pass a uint32_t to a function which defines the input type as uint64_t?


Pass a uint32_t to a function that requires uint64_t: You can do this, and all compilers I know accept it, even in strict modes.

  • Is it explicitly allowed by the standard?
  • Is it safe?
  • What does MISRA-C say about this?
void MyFunction(uint64_t arg) {}

int main(void) {
    uint32_t xyz = 1u;
    MyFunction(xyz);
}

Solution

    • uintN_t types are in the C standard library C99 and later.

    • Optional types uint32_t and/or uint64_t might not exist, yet that is only on very unusual machines. (E. g. CHAR_BIT == 9)

    • Code is missing #include <stdint.h>


    Is it safe to pass a uint32_t to a function which defines the input type as uint64_t?

    Yes. Yet see above notes.

    Is it explicitly allowed by the standard?

    Yes.

    Is it safe?

    Yes.

    What does MISRA-C say about this?

    Type sign-ness and value changes are things MISRA and well enabled compilers whine about.

    A sign-ness/value change does not occur in OP's code.