C programming: error checking with scanf and while loops

Опубликовано: 07 Январь 2025
на канале: Painless Programming
4,608
58

C programming: Doing error checking with scanf.

scanf will return the number of values successfully read in. If the user types a letter instead of a numbers, scanf will return 0.

You can add a while loops to keep re-prompting until the user types a number instead of a letter to avoid an infinite loop if the user accidentally types in the wrong data type.