Usually, when an expression has a specific numeric type, and its context expects another numeric type, the corresponding conversion will happen automatically. This applies in cases such as:
void f(float); void g(long); int i = 3; f(i); g(i); double d = i;
This principle also applies to the return .
This principle does not apply to variational functions such as printf . The reason is that the correct conversions depend on the format string, and the compiler is not required to parse the format string; in fact, if the format is not a string literal, the compiler usually cannot parse it. So, all throws are needed in the following printf call:
int i = 3; float f = 1.5; printf("%ld %f %d\n", (long)i, (double)i, (int)f);
However, due to promotions ( char and short rises to int , float advances to double ) and to the fact that %c expects int , there are no throws in the next printf call:
char c = 'a'; float f = 1.5; printf("%c %d %f", c, c, f);
source share