Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

float_t and double_t format specifiers

There are new format specifiers for intN_t types, for example %"PRIiN" and %"SCNiN", for printf and scanf families of functions.

What are, if any, the new format specifiers for float_t and double_t? (defined in math.h)

Can I -safely- use %f and %lf? I don't think so, because float_t is only at least as large as float, but could be defined as long double.

As nobody answered, and I don't find the answer anywhere, may it be a bug in C?

like image 655
alx Avatar asked Nov 08 '22 10:11

alx


1 Answers

To be cautious and portable you could us %Lf in your printf control string and cast values of type float_t or double_t to long double.

Edited: Format specifier for long double is %Lf and not %lf

You have to be more cautious for scanf because in that case casting won't help you.

Alternatively you could define your own printf and scanf format specifiers for float_t and double_t, making use of the FLT_EVAL_METHOD[1] macro to find out which built-in types that float_t and double_t are respectively equivalent to.

like image 137
user3405743 Avatar answered Nov 15 '22 04:11

user3405743