For the following code,
#include <stdio.h>
#include <limits.h>
#include <float.h>
int main(void) {
printf("double max = %??\n", DBL_MAX);
printf("double min = %??\n", DBL_MIN);
printf("double epsilon = %??\n", DBL_EPSILON);
printf("float epsilon = %??\n", FLT_EPSILON);
printf("float max = %??\n", FLT_MAX);
printf("float min = %??\n\n", FLT_MIN);
return 0;
}
what specifiers would I have to use in place of the ??'s in order for printf to display the various quantities as appropriately-sized decimal numbers?
Use the same format you'd use for any other values of those types:
Arguments of type
float
are promoted todouble
for variadic functions likeprintf
, which is why you use the same format for both.%f
prints a floating-point value using decimal notation with no exponent, which will give you a very long string of (mostly insignificant) digits for very large values.%e
forces the use of an exponent.%g
uses either%f
or%e
, depending on the magnitude of the number being printed.On my system, the above prints the following:
As Eric Postpischil points out in a comment, the above prints only approximations of the values. You can print more digits by specifying a precision (the number of digits you'll need depends on the precision of the types); for example, you can replace
%g
by%.20g
.Or, if your implementation supports it, C99 added the ability to print floating-point values in hexadecimal with as much precision as necessary:
But the result is not as easily human-readable as the usual decimal format:
(Note:
main()
is an obsolescent definition; useint main(void)
instead.)