Does float behavior in printf undefined?

The C99 standard refers to double s in the specification for fprintf(which subsequently applies to printf). It states that the argument " doublerepresenting a floating point number is converted ..." Then, in paragraph 9 it says:

If the conversion specification is not valid, the behavior is undefined. If any argument is not the correct type for the corresponding specification, the behavior is undefined.

Therefore, I would expect the following behavior to be undefined, but my compiler does not warn about this.

double d = 2.0;
float f = 2.0f;
printf("%f", d);
printf("%f", f); // here

On the other hand, the specification for fscanfsays "floating point number" instead of double. This is undefined behavior, as this user claims?

+4
source share
2 answers

Passing floatin printfis not undefined behavior - it is simply not possible. floatwill advance to doublebefore you printfreceive it.

scanfdifferent in that you (at least usually) passing to scanfare pointers, not the data objects themselves. Since you are passing a pointer to the source data, scanfyou need to distinguish between the floatand characters double.

+4
source

floatin vararg functions it always rises to double.

scanf deals with pointers.

+4
source

All Articles