In the posted code there is no reason to use scanf
nor scanf_s
in the swap
function.
scanf_s
is a function introduced by Microsoft as a supposedly safer(1) alternative to scanf
that is too often used carelessly by unsuspecting programmers, especially for %s
, %[
and %c
conversions, leading to security flaws.
scanf_s
was standardized as an optional extension with subtly different semantics. Programs using scanf_s
are thus not portable.
Microsoft modified their C toolchain to push programmers to use scanf_s
, issuing warnings or errors whenever they encounter scanf
references. This is the cause for the message shown in the screenshot.
In your program, scanf_s
and scanf
would behave essentially the same way as the conversion %d%d
has the same semantics for both functions, yet scanf_s
would detect and handle null pointer arguments for %d
(causing the program to exit) whereas scanf
would just have undefined behavior (causing the program to exit with a segmentation fault).
The swap
function can be written this way:
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
Note also that main
should have an int
return type.
(1) This subject is quite sensitive as commented by Andrew Henley: scanf_s
is a function introduced by Microsoft as a vendor-specific, non-portable, not-any-safer alternative to scanf
that results in vendor-lock-in for your code base. An illustration of their internal EEE strategy.