Multiple choice

/* Read a double value from fp. */ double read_double (FILE * fp) { double d; assert(fp != NULL); fscanf(fp, %lf, d); return d; }

The code above contains a common error. Which one of the following describes it?

  1. fscanf() will fail to match floating-point numbers not preceded by whitespace.

  2. The format specifier %lf indicates that the corresponding argument should be long double rather than double.

  3. The call to fscanf() requires a pointer as its last argument.

  4. The format specifier %lf is recognized by fprintf() but not by fscanf().

  5. D must be initialized prior to usage.

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

The function fscanf() requires pointers to variables so it can store the parsed input values into them. Because 'd' is passed by value as a double rather than by reference as a pointer (such as &d), the function cannot update it properly and will cause undefined behavior.