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().
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Nested printf (printf inside printf) in C
- Use of & in scanf() but not in printf()
- How to print % using printf()?
- Return values of printf() and scanf() in C/C++
- How to change the output of printf() in main() ?
- Passing NULL to printf in C
- Cin-Cout vs Scanf-Printf
- Execution of printf with ++ operators
- What is the difference between printf, sprintf and fprintf?
- puts() vs printf() for printing a string
- Do not use sizeof for array parameters
- Can we use function on left side of an expression in C and C++?
- Use of realloc()
- When do we use Initializer List in C++?
- Use of explicit keyword in C++
- How to use getline() in C++ when there are blank lines in input?
- Use of fflush(stdin) in C
- How to use POSIX semaphores in C language
- Why strcpy and strncpy are not safe to use?
- Why to use fgets() over scanf() in C?


