Wrong format specifiers in scanf (or) printf

前端 未结 1 1726
我在风中等你
我在风中等你 2020-11-27 08:00

As of the printf function is concerned, I understand the following from few references and experiments.

  • When we try to print an integer value with
1条回答
  •  遥遥无期
    2020-11-27 08:21

    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.

    0 讨论(0)
提交回复
热议问题