Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
hello again,
Is it possible for the user to specify how many decimal places to format a number in C? I could use switch but the user should be able to specify and number. For example:
fprintf(STDOUT, "%4d", num);
prints a number to 4 decimal places. Or how do I get the fprintf function to print the number of places in decplaces?
int decplaces;
.....
.....
fprintf(STDOUT, "%d", num);saddam

Mr. Ex-President,
I'll only answer your last question and that'll make everything clear. Use -
fprintf(stdout, "%*d", decplaces, num);
Hope you've got what you want. Best of luck.Santanu Sen
National Institute of Technology
Durgapur
India

hello,
Many thanks, you are the best!! I never thought both my questions would be answered by the same person! As of the pervious one no one seem to answer it and it was beginning to slip away.
I am sorry but could you explain what the * is all about and what it does?
thanks again.
saddam

Hi,
It's really great to have such complements from an ex-president. Anyways, let me explain what you want to know.When you write some code segment like
int a = 10;
fprintf(stdout, "%15d", a);it prints the value of 'a' using 15 places. That is it will print 13 blanks then '10'. You already know that.
Now if you write
int a = 10;
int b = 15;fprintf(stdout, "%*d", b, a);
It will generate the same output as the previous one. You may visualize it this way, first it replaces the * sign of the %*d flag with the value of 'b'. That is, it becomes %15d. Now it will print the value of a using this precision. Similarly, were the value of 'b' 8 it would have printed the value of 'a' using %8d. Note that the value of 'b' is not printed. It is only used to set the precision.
Hope I am clear enough. Best of luck.
Santanu Sen
National Institute of Technology
Durgapur
India

![]() |
how to use smbclient in j...
|
Java Copy and Paste Probl...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |