Hi,
I was wondering if it is possible to use a defined constant, like
In a printf format, likeCode:#define PRECISION 3
How to define a number's precision by a constant?Code:printf("%.PRECISIONf", SomeFloatVariable);
Hi,
I was wondering if it is possible to use a defined constant, like
In a printf format, likeCode:#define PRECISION 3
How to define a number's precision by a constant?Code:printf("%.PRECISIONf", SomeFloatVariable);
You can specify the precision as an extra argument using '*' instead of the actual width e.g.
There's a variant that allows you to use a numbered argument [CORRECTED (I hope) - thanks Bachstelze!] e.g.Code:printf("%.*f\n", PRECISION, floatval);
Code:printf("%1$.*4$f %2$.*5$f %3$.*4$f\n", floatval, floatval, floatval, PRECISION, PRECISION_ALT);Code:#include <stdio.h> #define M_PI 3.14159265358979323846264338327 #define PRECISION 3 #define PRECISION_ALT 6 int main() { printf("%.*f\n", PRECISION, M_PI); printf("%1$.*4$f %2$.*5$f %3$.*4$f\n", M_PI, M_PI, M_PI, PRECISION, PRECISION_ALT); return 0; }- see the printf man page (man 3 printf)Code:$ gcc -std=c99 -Wall -o prec prec.c $ $ ./prec 3.142 3.142 3.141593 3.142 $
Last edited by steeldriver; November 26th, 2013 at 11:40 PM.
You can take advantage of the automatic abuttal of string constants if PRECISION is a string literal (which make this more suitable for whole formats):
But in most cases you should be using a precision parameter as indicated above.Code:#include <stdio.h> #define FORMAT "%02x" int main() { printf("The answer is : "FORMAT"\n",66); }
Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Another option
I'd normally prefer steeldriver's first suggestion though.Code:#define STRINGIZE(x) #x #define PRECISION 3 ... printf("%." STRINGIZE(PRECISION) "f", some_double);
The %n$ thing isn't just nonstandard; it's uncommon, and most C programmers have probably never heard of it, which means that unless it buys you something serious in clarity or performance, it's probably not worth the maintenance cost.
The things this format buys you is multi-language support, if the format string is a message to the user. Depending on language the substituted elements may not appear in the same order in the sentence.
Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Big thanks to you guys, I almost get it now. I just don't understand that "dollar" part. The beginning of the steeldriver's post seemed to solve my problem, but then this appeared:
Couldn't it be written in this way:Code:printf("%1$.*4$f %2$.*5$f %3$.*4$f\n", M_PI, M_PI, M_PI, PRECISION, PRECISION_ALT);
?Code:printf("%.*f %.*f %.*f\n", PRECISION, M_PI, PRECISION_ALT, M_PI, PRECISION, M_PI);
And, if we follow that dollar method, why isn't it in this way:
?Code:printf("%1$.*2$f %1$.*3$f %1$.*2$f\n", M_PI, PRECISION, PRECISION_ALT);
Also I wonder how that first one worked. For example:
You give it first argument at the start, but it refers to the whole number to display not precision. A second "dollar" tells the precision but it's closer to the end of the format...Code:%1$.*4$f
I'm not sure if you know what I mean. You can simply avoid that last question if you like
Last edited by ppplayer80; November 27th, 2013 at 05:02 PM.
All of those do the same thing.
In general I will agree with trent.josephsen that the $ construct does more harm than good. It's one of those things that seem to have been invented only to make a hacker's life easier (the only place I have ever seen it used in practice is in format-string-based attacks).
Bookmarks