Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what does the format specifier %n do?

Status
Not open for further replies.

asavitha

Technical User
Jun 23, 2003
1
0
0
IN
i tried executing the following program:
main()
{
int i=250;
printf("%n",&i);
printf("...%d",i);
}
and got the output as...1
the output is same for any value of i
please explain me the output and the function of format specifier %n
Thank u for any help
 
It doesn't do anything. If printf does not understand a specifier, it doesn't do anything. If you want the address, use %p.
 
savitha,

%n :
u have used "not a so good" code to see the effect..
suppose..
Code:
    int char_count;

    printf("this%nis a test\n", &char_count);
    printf("%d", char_count);

   printf("this is a test%ndemo \n", &char_count);
   printf("%d", char_count);

    printf("this is a test demo%n\n", &char_count);
    printf("%d", char_count);

Check the output .. what it does is , it writes into that integer pointer char_count the no. of characters written so far.

 
Incidentally, if %n weren't specified by the C standard (it is, as manvid points out), we don't know how printf will behave when encountering it. The implementation might provide a meaning for it or it might not. Your program might conceivably crash if you pass printf an unrecognized conversion specifier.
 
dlkdlk,
oh yup!
if the character(s) following "%" is not a conversion character, the behaviour is unpredictable ..

and BTW savitha, the %n can be used by scanf also.. ofcourse i suppose itz obvious over what it shud be doing.. the no. of characters it has read so far.. tho' i must confess i have never used it :)
-vs
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top