Was ist der Unterschied zwischen scanf und scanf_s? In der Universität wurde ich unterrichtet und ich benutze scanfaber auf meinem PC sendet Visual Studio weiterhin diese Warnung.
error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead.
Und ich muss alles ändern scanf zu scanf_s oder das Programm wird nicht erstellt. (Ich verwende Visual Studio 2013)
mögliches Duplikat von Scanf_s Warnung? Überspringt Benutzereingaben (Themen: Runge-Kutta, Epidemie-Simulation)
– Andreas Fester
29. Januar 2014 um 15:01 Uhr
Es ist dasselbe wie scanfaußer dass es keine Pufferüberlastung verursacht.
Es ist eine Funktion, die speziell zum Microsoft-Compiler gehört.
scanf Liest ursprünglich nur die von Ihnen eingegebene Konsoleneingabe und weist sie einem Variablentyp zu.
Wenn Sie ein Array namens haben first_name[5] und du verwendest scanf für „Alex“ kein Problem. Wenn Sie dasselbe Array haben und “Alexander” zuweisen, können Sie sehen, dass es die 5 Slots überschreitet, die das Array enthält, sodass C es immer noch in Speicher schreibt, der nicht zum Array gehört, und das Programm möglicherweise abstürzt oder nicht , abhängig davon, ob etwas versucht, auf diesen Speicherplatz zuzugreifen und darauf zu schreiben, der nicht zu first_name gehört. Das ist wo scanf_s kommt herein.
scanf_s hat ein Argument (Parameter), mit dem Sie die Puffergröße angeben und das Limit der Eingabe tatsächlich steuern können, damit Sie nicht das gesamte Gebäude zum Absturz bringen.
“wo Sie die Puffergröße angeben können” -> das ist falsch. Richtig: “wo Sie die Puffergröße angeben können und Sie sie für alle Eingabeparameter vom Typ c, C, s, S oder angeben müssen [.” Therefore, simply adding postfix “_s” to programs that used scanf(“…%s…”, …) leads to a program, which compiles with NO WARNING, but DOES NOTHING, since 0-size buffer is assumed and no data are read in.
– Palo
Dec 21, 2015 at 21:28
Actually, in Visual Studio, the compiler complians whenever %s or %c is used without a buffer length argument.
– Anders Marzi Tornblad
Jun 17, 2016 at 12:08
The _s functions are now part of C11 and portable.
– Dai
Oct 30, 2016 at 3:32
The _s functions are now part of C11 and portable. Annex K is optional, and the Microsoft implementation is not portable. Field Experience With Annex K — Bounds Checking Interfaces: “Microsoft Visual Studio implements an early version of the APIs. However, the implementation is incomplete and conforms neither to C11 nor to the original TR 24731-1. … As a result of the numerous deviations from the specification the Microsoft implementation cannot be considered conforming or portable.”
– Andrew Henle
Feb 22, 2019 at 16:49
scanf_s() is not described by the C99 Standard (or previous ones).
If you want to use a compiler that targets C99 (or previous) use scanf().
For C11 Standard (and eventually later ones) scanf_s() is much harder to use than scanf() for improved security against buffer overflows.
If you have a C99 compiler with extras that provides scanf_s() as an extension and don’t mind losing portability, check your compiler documentation.
Note that Annex K of the C11 Standard is optional. An implementation can claim to be C11 and not provide scanf_s() (you can check with #if defined(__STDC_LIB_EXT1__)).
– pmg
Feb 14, 2016 at 17:43
I would not call it “much harder” to use. It’s simply a matter of providing buffer sizes whenever scanning for strings och characters, which you should already be doing anyway!
– Anders Marzi Tornblad
Jun 17, 2016 at 12:07
What you can do to avoid this error is to paste the thing between the <>:
<_CRT_SECURE_NO_WARNINGS>
to a place.
To get to the place right click on your project in the solution explorer and click on the properties. then go to the configuration properties, then the c/c++, then the preprocessor. then in preprocessor definitions, after everything, add a semicolon and paste the thing in. then press apply and ok. Your problem should be solved.
13870700cookie-checkUnterschied zwischen scanf und scanf_syes
mögliches Duplikat von Scanf_s Warnung? Überspringt Benutzereingaben (Themen: Runge-Kutta, Epidemie-Simulation)
– Andreas Fester
29. Januar 2014 um 15:01 Uhr
Es ist dasselbe wie
scanf
außer dass es keine Pufferüberlastung verursacht.– David Ranieri
29. Januar 2014 um 15:02 Uhr
sehen msdn.microsoft.com/en-us/library/w40768et(v=vs.90).aspx,
– BLUEPIXY
29. Januar 2014 um 15:04 Uhr