Views
Dynamic Output in C
- 12 Comment
Welcome to my blog :-) .If you're new here, you may want to subscribe to my full RSS feed. Thanks for visiting!
Dynamic output
consider the following program
#include<stdio.h>
int main()
{
long double a=3.14159265;
printf(”Value of pi=%.4Lf”,a);
return(1);
}
the output of the program will be
Value of pi=3.1416
wouldn’t it be nice if we allow the user to choose how the precession of the answer would be??
that is the output should be like this
Enter precision
6
The value of pi precised to 6 decimal number is 3.141593
Well in that case the program will be like this
#include<stdio.h>
int main()
{
long double a=3.14159265;
int pre;
printf(”Enter precision\n”);
scanf(”%d”,&pre);
printf(”The value of pi precised to %d decimal number is %.*Lf”,pre,pre,a);
return(1);
}
see the difference,it is upto the user to decide how the output will be.The value of pi will be rounded off to fit the precision .Such an output is called dynamic output.
This is the part of the program which is important
printf(”The value of pi precised to %d decimal number is %.*Lf”,pre,pre,a);
you can create a dynamic output by just specifing * before control string and specifying the variable having precesion in the arguments list.As you can see i have specified pre twice because one to print the The value of pi precesiced to__ and the other to suggest the compiler to fill the * with the value of pre.
This fact can not only be used to specify the precision of the decimal number but also for various other things.Some of them are
1.Printing first n characters of the given string
consider
printf(”The Output is %.*s”,pre,str);
suppose the string variable str has a string of “adithya” and the value of int datatype variable pre is 3 then the output will
The Output is adi
2.Justification
consider
printf(”The Output is %*.*f.Bye!”,just,pre,num);
suppose num(float)=1983.78
pre(int datatype variable)=2
just(int datatype variable)=25
then the output will be in the form of right justification i.e,
The Output is 1983.78.Bye!
suppose you want the output to be left justified then either you can specify ‘-*.*f’ or give a negative value to the variable ‘just’
suppose for the above example just=-25 then the output will be
Code:
The Output is 1983.78 .Bye!
You may also like
12 Comments on this post
Trackbacks
-
Supriya said:
Nice tutorial and good blog. Hope you will post more tutorials like this. Definitely useful.
March 6th, 2008 at 10:22 am -
adithya said:
thanks Supriya..
It’s been a long time since i had a visitor…..
Will add new contents and tutorials soon…..currently busy with exams
March 6th, 2008 at 11:27 am -
Supriya said:
oh! I just read the about page and you are doing your engineering. Thats Good! Have a nice time. And which college you are? I think only malnad is famous in hassan district. Even i’m quite busy in my studies. I’m doing my 1st year BCom and learning C programming for extra skills, looking forward for more tutorials from you.
March 6th, 2008 at 12:24 pm -
adithya said:
Yup i’m from Malnad College Of Engineering….
Glad to know about you…
And yes there will be lots of tut’s and articles which will be posted soon….
and BTW till then you can try out the software i created..
Presenmaker 1.0 –>link:http://blog.aditech.info/2008/02/16/presenmaker-10/March 6th, 2008 at 1:02 pm -
Supriya said:
oh that was good. Its built on VB.NET rite ?
Nice concept, and u are the next bill gates ?
March 6th, 2008 at 2:33 pm -
adithya said:
lol

Thanks for the appreciation…and yes it’s VB.NET
I’m designing another software which has text to speech and text to mp3 option…it will be launched soon…
And i am planning to give a tut on how to do that also..
March 6th, 2008 at 2:37 pm -
adithya said:
“I am very curious to know how you came to my site..?
did you come via google..?
if yes then what is the search term you used..?
please do let me know how did you know about my site coz I have done nothing to promote my site…
your answer might help me more in my SEO (Search Engine Optimization) work….”
i tried to contact you to know these via your email but i didn’t got any responses for it…please do reply the above…
and BTW i have added smiles support for the comments..
March 6th, 2008 at 2:41 pm -
Supriya said:
Oh thats fantastic, so it makes u richer than bill gates. And BTW nice smilies. How to install a wordpress on our server? I dont want to upload and install it by manual method. Can u help me? If possible, will u mail it to my ID ? Or if there is any tutorial please give the link here?
March 6th, 2008 at 2:46 pm -
adithya said:
Installing wordpress is easy ….

and smiles too..
i can help you but i am currently busy with exams…
exams(internals) start from tomorrow…
you can contact me at adithyau[at]gmail[dot]com
or Y! chat at adithyau[at]yahoo[dot]co[dot]inMarch 6th, 2008 at 2:53 pm -
Supriya said:
oh, i replied to the mail
March 6th, 2008 at 3:00 pm -
Supriya said:
Thanks adithya
March 6th, 2008 at 5:40 pm -
Rag said:
gud work adi!
and u have extreme C skills!!!!!!!
its gud tht i got to know this site. keep posting articles which help me in my placements. thk u.August 9th, 2008 at 12:30 pm

