On my machine:
- size_t s = 1;
-
printf("%llu\n", s);
-
printf("%llu\n", (unsigned long long) s);
the execution result is:
That is UB. Unless you step in and force s to be an unsigned long long, this printf function croaks. printf ASSUMES %llu is safe to display the argument as an unsigned long long. Apparently, unless you jump in and tell the compiler to use it as an unsigned long long, you get a bad result from printf.
The typecast has broken type safety. The display is a unsigned long long but the program is using another variable of type size_t.
Now I realize that weaknesses in C force you to cast all the time. Check this out:
-
const int var = 10;
-
int* ptr = &(int)var;
-
*ptr = 25;
-
printf("%d\n %d\n", var, *ptr);
-
On my machine I get
I was able to change a const int from 10 to 25 by typecasting the address of a const int to be the address of an int. Then by using a pointer to int, the value of the const was changed.
So the program is using a const of 10 for whatever until the typecast when it now starts using 25. You have no idea when you make a function call using this varable whether it is a const 10 or a const 25.
C++ won't even compile this code.