Disadvantages of scanf

前端 未结 9 1172
傲寒
傲寒 2020-11-22 00:34

I want to know the disadvantages of scanf().

In many sites, I have read that using scanf might cause buffer overflows. What is the reason f

9条回答
  •  青春惊慌失措
    2020-11-22 01:13

    There is one big problem with scanf-like functions - the lack of any type safety. That is, you can code this:

    int i;
    scanf("%10s", &i);
    

    Hell, even this is "fine":

    scanf("%10s", i);
    

    It's worse than printf-like functions, because scanf expects a pointer, so crashes are more likely.

    Sure, there are some format-specifier checkers out there, but, those are not perfect and well, they are not part of the language or the standard library.

提交回复
热议问题