Wrong format specifiers in scanf (or) printf

Variadic arguments (those matching the ellipsis, ...) are default-promoted. That means that all shorter integral types are promoted to int (or unsigned, as appropriate). There’s no difference between inte­gers and characters (I believe). The difference between %d and %c in printf is merely how the value is formatted.

scanf is a different kettle of fish. All the arguments you pass are pointers. There’s no default-pro­mo­tion among pointers, and it is crucial that you pass the exact format specifier that matches the type of the pointee.

In either case, if your format specifier doesn’t match the supplied argument (e.g. passing an int * to a %p in printf), the result is undefined behaviour, which is far worse than being “unpredictable” — it means your program is simply ill-formed.

Leave a Comment