joshi joshi - 3 months ago 10
C Question

What is the use of the %n format specifier in C?

What is the use of the

%n
format specifier in C? Could anyone explain with an example?

Answer

Nothing printed. The argument must be a pointer to a signed int, where the number of characters written so far is stored.

#include <stdio.h>

int main()
{
  int val;

  printf("blah %n blah\n", &val);

  printf("val = %d\n", val);

  return 0;

}

The previous code prints:

blah  blah
val = 5