%n format specifier program giving different outputs on different compilers. Why?

后端 未结 2 1551
心在旅途
心在旅途 2021-02-12 16:41

I was reading about %n format specifier in C at this question. But when I tried the following program on different C++ compilers, it gave me different outputs.

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-12 17:33

    The Code Blocks output is correct.

    The Orwell Dev C++ output is incorrect. Either that implementation is not non-conforming by default (read the documentation to see if there's a way to make it behave correctly), or it has a bug.

    Microsoft's implementation is non-conforming by default. It disables the standard %n format specifier to prevent some possible security problems (though there are no such problems in the code in your question). Apparently there are ways to re-enable it; see Shafik Yaghmour's answer.

    The only potential problem I see with your program is that it doesn't print a newline at the end of its output, but that's not relevant to the %n issue.

提交回复
热议问题