Home > database >  Meanning of %! and %K with printf
Meanning of %! and %K with printf

Time:07-14

I try to rewrite printf function and i found a strange result when use format specifier (%) with ! or K. I want to understand why i get this result.

    printf("%!");
    printf("%K")

I get output ! and K. Thank for all response

CodePudding user response:

According to §7.21.6.1 ¶9 of the ISO C11 standard, using an invalid conversion specification will result in undefined behavior.

Therefore, you cannot rely on any specific behavior. Anything may happen. On different compilers, the behavior may be different. Even on the same compiler the behavior may change, if you for example update the compiler to a different version or compile with a different optimization level.

If you want the behavior to be well-defined, so that you can rely on a specific behavior, then you should only use valid conversion specifiers. You can use the %% conversion specification to print a literal %.

CodePudding user response:

printf("%!"); printf("%K") are both undefined behavior. Any result is possible. As part of OP's rewrite, code can do anything in these cases.

  • Related