A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
In printf, %f and %lf are identical. The float argument is promoted to double by the variadic argument rules, so printf always receives a double. The l modifier is ignored. In scanf, %f reads a float and %lf reads a double. Here the distinction matters. Using %f to read a double writes 4 bytes into an 8-byte variable, corrupting the adjacent memory. Using %lf to read a float writes 8 bytes into a 4-byte variable, overflowing it.
#include <stdio.h>int main(void) {double d = 3.14159;float f = 2.718f;printf("printf %%f: %f\n", d);printf("printf %%lf: %lf\n", d);printf("printf %%f: %f\n", f);double input_d;float input_f;sscanf("1.414", "%lf", &input_d);printf("scanf %%lf into double: %.3f\n", input_d);sscanf("1.732", "%f", &input_f);printf("scanf %%f into float: %.3f\n", input_f);return 0;}
printf treats %f and %lf identically. Both print the double correctly. scanf treats them differently: %lf writes 8 bytes for a double, %f writes 4 bytes for a float. The rule is simple: in printf, use %f for both float and double. In scanf, use %f for float and %lf for double. Mixing them in scanf is a memory corruption bug.
printf: %f for everything floating-point. scanf: %f for float, %lf for double. The asymmetry exists because printf receives promoted arguments while scanf writes to the addresses you provide. Remember the direction of data flow and the rule follows. Next: an introduction to threads with pthreads, where C programs do multiple things at once.
Finish the lesson once you have worked through the material. This awards ★ 20 XP.