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

Discussion

In a C program, following variables are defined:

float      x = 2.17;

double   y = 2.17;

long double z = 2.17;

Which of the following is correct way for printing these variables via printf.

  • A.printf("%f %lf %Lf",x,y,z);
  • B.printf(“%f %f %f”,x,y,z);
  • C.printf("%f ? ?f",x,y,z);
  • D.printf("%f %lf %llf",x,y,z);

Answer: A

Correct answer: printf("%f %lf %Lf",x,y,z);

A float passed to a variadic function is promoted to double, so %f serves for both float and double, and %lf is accepted as a synonym for the same. A long double is not promoted and needs its own specifier, %Lf with a capital L; using %f for it reads the wrong number of bytes.

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

Post your comment

Please log in or register to post a comment.