Thursday, 8 November 2012

format specifier %n


The %n Specifier:



The %n format specifier is different from the others. Instead of telling printf( ) to display
something, it causes printf( ) to load the integer variable pointed to by its corresponding argument
with a value equal to the number of characters that have been output. In other words, the value that
corresponds to the %n format specifier must be a pointer to a variable. After the call to printf( ) has
returned, this variable will hold the number of characters output, up to the point at which the %n
was encountered. Examine the next program to understand this somewhat unusual format code:





#include <stdio.h>
int main(void)
{
int count;
printf("this%n is a test\n", &count);
printf("%d", count);
return 0;

output:

this is a test
followed by the number 4.

No comments:

Post a Comment