Lesson 73 +10 XP

Output with Specifiers for Input

Output with Format Specifiers for Input

The specifiers from printf are the same ones used in scanf - and they must match the type.

The specifier table

int i;      // in: %d
float f;    // in: %f
double d;   // in: %lf
char c;     // in: %c
char s[50]; // in: %s

Echo it back

scanf("%d", &i);
printf("%d\n", i);

The right specifier means correct behavior

Mismatched types produce garbage. %lf is the double form; %f is float.

Mixed types in one scan

scanf("%d %f %d", &price, &rate, &year);

TL;DR

  • printf and scanf share the same specifiers.
  • %d int, %f float, %lf double, %c char, %s string.
  • Match each specifier to the variable's type.
  • Garbage in, garbage out with mismatched types.