Introduction / Context:
Correct conversion specifiers are essential when using scanf in C to avoid undefined behavior and corrupted inputs. In particular, float and double have different specifiers in scanf because arguments are not promoted the same way at input time as they are at output time with printf.
Given Data / Assumptions:
- Variables are declared as float a; and double b;.
- We must read both in one scanf call.
- Standard C conversion specifiers apply.
Concept / Approach:
- For scanf, use %f to read into a float* and %lf to read into a double*.
- %Lf is used for long double* with scanf.
- Although printf promotes float to double, scanf requires exact matching of pointed-to types.
Step-by-Step Solution:
Match a (float) → %f.Match b (double) → %lf.Therefore, the correct call is scanf("%f %lf", &a, &b);
Verification / Alternative check:
Attempting scanf("%f %f", &a, &b) misreads b because %f expects float*, not double*.
Why Other Options Are Wrong:
- "%f %f": second %f does not match a double*.
- "%Lf %Lf": %Lf expects long double*, not float* or double*.
- "%f %Lf": first is correct for a; second expects long double* but b is double.
Common Pitfalls:
- Confusing printf and scanf specifiers; they are not symmetric.
- Forgetting address-of operators (&) when passing variables to scanf.
Final Answer:
scanf("%f %lf", &a, &b);
Discussion & Comments