Understanding implicit conversions for printf

前端 未结 2 1670
终归单人心
终归单人心 2021-01-25 04:44

The C99 Standard differentiate between implicit and explicit type conversions (6.3 Conversions). I guess, but could not found, that implicit casts are performed, when the target

2条回答
  •  旧巷少年郎
    2021-01-25 05:00

    Arguments to va_arg functions are not converted, syntactically the compiler knows nothing about the arguments for such functions, so he can't do that. Modern compilers do know to interpret the format string, though, and so they are able to warn you when something fishy is going on. That's what happening when you see the warning from gcc.

    To be more precise, there are some promotions that are done for narrow integer types, they are promoted to int, and for float which is promoted to double. But that is all magic that can happen, here.

    In summary, always use the correct format specifier.

    BTW, for size_t as of your sizeof expressions the correct one is %zu.

提交回复
热议问题