Yesterday one of my friends asked me about %n,i got shocked because i have nvr hear abt that...after searching about %n in printf i come up with the follwing info...
What is does::
1.)It Prints nothing,
2.)But write number of characters successfully written so far into an integer pointer parameter..
We will see the ouput by using printf,fprintf and sprintf ,the concept is same it nvr prints to anything whether it is :
#include<conio.h>
int main()
{
FILE *fp;
int i=10,j=20,k=30;
int *p;
fp=fopen("c:\\test.txt", "w");
fprintf(fp, "Testing...%d%d%d%n",i,j,k,p);
printf("done....");
printf("\nnumber of characters written to the file are %d",*p);
getch();
return 0;
}
#include<conio.h>
int main()
{
char buff[50];
int a=34,b=50,*p;
clrscr();
sprintf(buff, "%d minus %d equals %d%n",a,b,a-b,p);
printf ("(%s) is the result of our sprintf",buff);
printf("\nNumber of characters written to the buff are:%d",*p);
getch();
return 0;
}
output:
What is does::
1.)It Prints nothing,
2.)But write number of characters successfully written so far into an integer pointer parameter..
We will see the ouput by using printf,fprintf and sprintf ,the concept is same it nvr prints to anything whether it is :
-->standard ouput(console) in case of printf.
-->a file in case of fprintf.
-->a string in case of sprintf
With printf
#include<stdio.h>
#include<conio.h>
int main()
{
int *i,*j;
clrscr();
printf("milan%nkumar%n",i,j);
printf("\ni=%d",*i);
printf("\nj=%d",*j);
getch();
return 0;
}
#include<conio.h>
int main()
{
int *i,*j;
clrscr();
printf("milan%nkumar%n",i,j);
printf("\ni=%d",*i);
printf("\nj=%d",*j);
getch();
return 0;
}
output:
milankumar
milankumar
i=5
j=10
Explanation:
let us take the format string in the printf.it is "milan%nkumar%n" whenever printf find the %n it looks for the integer pointer argument and writes the numers of characters sucessfully written so far at its location,so,when printf find the first %n it sets i with number of character written on the stdout that is 5(milan)...now finding the another %n ,printf sets the j with the number of characre written so far on the stdout that is 10(milankumar)..
here we can see that %n nvr prints anything to the stream associated with the function...
With fprintf
#include<stdio.h>#include<conio.h>
int main()
{
FILE *fp;
int i=10,j=20,k=30;
int *p;
fp=fopen("c:\\test.txt", "w");
fprintf(fp, "Testing...%d%d%d%n",i,j,k,p);
printf("done....");
printf("\nnumber of characters written to the file are %d",*p);
getch();
return 0;
}
Output:
Testing...102030 /*data written to file:*/
done/*written to console*/
number of characters written to the file are 16 /*written to console*/
Explanation:
This example clears that %n is only associated with the printf in which it is used,it is not hardley attched with the stdout or printf ,it will always set the integer pointer with the number of the characters written to the stream that is used by the function..stream many be file,stdout,string etc..
so number of characters written to the file are 16(Testing...102030)..hence the output...
With sprintf
#include <stdio.h>#include<conio.h>
int main()
{
char buff[50];
int a=34,b=50,*p;
clrscr();
sprintf(buff, "%d minus %d equals %d%n",a,b,a-b,p);
printf ("(%s) is the result of our sprintf",buff);
printf("\nNumber of characters written to the buff are:%d",*p);
getch();
return 0;
}
output:
(34 minus 50 equals -16) is the result of our sprintf
Number of characters written to the buff are:22Explanation:
Now i think you can easily explain the output similarly in th eabove examples....
other wise feel free to mail me at milankmr@gmail.com
No comments:
Post a Comment
Feel free to comment......