On Habre says :

The qualifier "% n" does not take into account the number of characters derived from specifiers such as "% f". Therefore, we put one space before "% n" to write the value 1 to isOkPassword.

However, the verification shows that this is not the case at all:

#include <stdio.h> int main(void) { int n; printf("%f %n\n", 12.0, &n); printf("%d", n); return 0; } 
 12.000000 10 

And how should this code actually work?
Like %n for this purpose, it is necessary to take into account the formats, because otherwise there would be no point in it.

  • @StateItPrimitive, with the removal of the c ++ label does not agree. printf is in both languages. - Qwertiy ♦
  • From the point of view of the question, it is C- shny, but from the point of view of attracting attention, probably, the truth is to leave the label (for example, it is not uncommon to ask android for questions according to the java standard). Simply, I proceeded from the fact that a C ++ specialist is hardly obliged to know the peculiarities of the operation of certain C-shny functions / modifiers ... and therefore it is unlikely to immediately answer the question. - StateItPrimitive pm
  • On the cppreference, they write: "returns the number of characters written", there is no talk of ignoring the output text. However, I would not trust the articles on HabrĂ©. - VladD
  • @VladD, there we are not talking about ignoring spaces, but about ignoring output in the %f format (I suppose other formats are also implied). And this is a blog of Pvs Studio - a code analyzer - it’s somehow strange that they shouldn’t be trusted :( - Qwertiy ♦
  • one

1 answer 1

The %n causes the total number of characters sent to the output stream to be written to the receive argument ("... There is no “not taking into account” any format specifiers and there can not be.