Guided Notes

User Input & Math

Reading input with scanf and fgets, checking scanf's return value, mixing them safely, and the math.h library.

scanf

Slide 1 / 5

scanf reads formatted input into variables — pass the address with &: int age; scanf("%d", &age);

• %d int, %lf double, %c char, %s a word (stops at spaces). • Several values: scanf("%d %d", &a, &b); • Strings don't need & (an array is already an address), but limit the width: scanf("%49s", name) for char name[50].

scanf returns how many values it read — check it: if (scanf("%d", &age) != 1) { /* not a number */ }

← Strings