There are two reasons:
scanf() may leave stdin in a state that is difficult to predict; this makes error recovery difficult, if not impossible (this is less of a problem with fscanf() ); and- The entire
scanf() family accepts pointers as arguments, but does not limit the length, so they can intercept the buffer and change the unrelated variables that occur after the buffer, causing seemingly random memory corruption errors that are very difficult to understand, find, and debug , especially for less experienced C programmers.
C beginners often get confused about pointers and the address-of operator and often omit & where necessary, or add it "for good measure" where it is not. This leads to โrandomโ segfaults, which can be difficult for them. This is not a scanf() error, so I leave this on my list, but it is worth keeping in mind.
After 23 years, I still remember that it was a huge pain when I started programming in C and did not know how to recognize and debug such errors, and (like someone who spent years teaching C to beginners), it is very difficult to explain them to beginners who don't understand pointers and stack yet.
Anyone who recommends scanf() novice C programmer should be mercilessly destroyed.
OK, maybe not mercilessly, but some kind of flogging is definitely in order: o)
Emmet
source share