Monday, 18 December 2017

What is use of %n in printf() ?


What is use of %n in printf() ?

     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.


Example:
#include <stdio.h>
int main(int argc, char *argv[])
{
    int c;

    printf("velraj total 16 %n won't count after n. \n", &c); // upto n the word length is 16, this value will be saved in c variable
    printf("%d\n", c);
    return 0;
}

Output:
----- snip ---------
velraj total 16  won't count after n.
16
----- snip ---------

No comments:

Post a Comment