cprintfformat-string

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?


Solution

  • 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