The Wayback Machine - https://web.archive.org/web/20241231124059/https://www.geeksforgeeks.org/g-fact-31/
Open In App

What is use of %n in printf() ?

Last Updated : 09 Oct, 2019
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In C printf(), %n is a special format specifier which instead of printing something causes printf() to load the variable pointed by the corresponding argument with a value equal to the number of characters that have been printed by printf() before the occurrence of %n.




#include<stdio.h>
  
int main()
{
  int c;
  printf("geeks for %ngeeks ", &c;);
  printf("%d", c);
  getchar();
  return 0;
}


The above program prints “geeks for geeks 10”. The first printf() prints “geeks for geeks”. The second printf() prints 10 as there are 10 characters printed (the 10 characters are “geeks for “) before %n in first printf() and c is set to 10 by first printf().



Next Article

Similar Reads

three90RightbarBannerImg