Machine-translated by Google. Questions are authored in English.
Top

Discussion

To print out a and b given below, which printf() statement would you use?

float a = 3.14;
double b = 3.14;

  • A.printf("%f%f",a,b);
  • B.printf("%Lf%f",a,b);
  • C.printf("%Lf%Lf",a,b);
  • D.printf("%f%Lf",a,b);

Answer: A

Correct answer: printf("%f%f",a,b);

Both are printed with %f. In a call to a variadic function such as printf, the default argument promotions apply, and a float argument is promoted to double before it is passed - so printf receives two doubles and %f is correct for each.

There is therefore no %f for float and separate specifier for double; %f serves both. %Lf is for long double, and using it here would misread the argument and produce nonsense.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.