#include #include #include #include typedef struct Foo { int x; } Foo; typedef struct Bar { int y; } Bar; int main() { // Numeric casts are conversions int64_t x64 = INT32_MAX; // assign 4byte to 8byte just works x64 +=10; int32_t x32 = x64; // assign 8byte to 4byte will overflow printf("wide/narrow integer %" PRId64 " %" PRId32 "\n", x64, x32); float f32 = 100.9999; int32_t i32 = f32; // round down printf("float to integer %" PRId32 "\n", i32); int64_t ii64 = INT64_MAX; float ff32 = ii64; // round because limited precision int64_t iii64 = 4000000000; float fff32 = iii64; // not round because precise enough printf("integer to float (rounded) %" PRId64 " %f\n", ii64, ff32); printf("integer to float %" PRId64 " %f\n", iii64, fff32); // struct casts are compiler errors Foo f; /* Bar b = (Bar) f; */ /* compile error */ // int to pointer (and vice versa) casts are allowed but a bad idea // (there are prescribed ways to do this properly. More later) int64_t z; int64_t* g = &z; z = g; /* compiler warning */ z = (int64_t) g; /* no warnings */ // pointer casts are just to get around compiler int* p = malloc(sizeof(int)); /* probably a warning */ int* q = (int*)malloc(sizeof(int)); /* no warnings */ printf("pointer casts %p %p\n", p, q); }