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.
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.