Typecasting on C types

Is that even possible to typecast from c_uint to c_int or vice versa?
What is the standard way?

You can use the same casting operations as for all other Zig integers.

Use @intCast if you know that the values will always be in the range of the target type. E.g. attempting to cast -1 to c_uint, will result in safety checked undefined bahvior.
Use @bitCast if you want to have the same behavior as a C cast. This will keep all the bits the same, so e.g. -1 will cast to maxInt(c_uint).
Use std.math.lossyCast to clamp it to the output range. E.g. casting -1 to c_uint will result in 0.

4 Likes