Short answer: scanf
doesn't provide protection from arithmetic overflow, thus, it's unsafe to use.
Detailed answer:
The original issue begins with the Buffer Overflow problem of gets in C. Link: Issue of gets
and solution
It was because of gets
function that one of first most widespread worm was able to propagate itself throughout the internet. Because gets
overwrites the stack/memory allocated to variable used to store it. This leads to buffer overflow.
Scanf link: Disadvantages of scanf()
and its alternative
Unlike gets
, scanf
does provide safety with string buffers by limiting the size, but, it is not possible for arithmetic input. Arithmetic input will overwrite the stack buffer. Although scanf
provides a way to avoid buffer overflow problem with strings but, usually we (lazy programmers) won't specify the limits while using the scanf
, hence we wrote an alternative scanf_s
Other alternatives of scanf
are, strtol
, strtok
, and atoi
, among others.
Edit 1: Changed from sscanf
to scanf_s
in ...hence, we wrote an alternative...