【发布时间】:2021-11-28 12:38:19
【问题描述】:
交换值的程序:
#include <stdio.h>
#include <conio.h>
void swap(int *a, int *b);
void main()
{
int a, b;
printf("Enter two numbers: ");
scanf_s("%d%d", &a, &b);
printf("Before swap\n");
printf("a = %d, b = %d\n", a, b);
swap(&a, &b);
printf("After swap\n");
printf("a = %d, b = %d", a, b);
}
void swap(int *a, int *b)
{
printf("Enter the swapped numbers: \n");
scanf("%d%d", a, b);
}
为什么我应该在这里使用scanf_s 而不是scanf?
为什么会这样?
感谢您的帮助。
【问题讨论】:
-
您可以放心地忽略该警告。
scanf有很多问题,但总的来说,scanf_s也好不到哪里去。 -
你不应该使用
scanf_s()。 Field Experience With Annex K — Bounds Checking Interfaces:“微软为了提高 API 的采用率而弃用标准函数而引发的一个普遍谬误是,对标准函数的每次调用都必然是不安全的,应该用对“更安全”的 API 的调用来代替。 .. Microsoft Visual Studio 实现了 API 的早期版本。但是,实现不完整,既不符合 C11 也不符合原始 TR 24731-1。..." -
(cont) "...由于与规范有大量偏差,Microsoft 的实现不能被认为是符合规范的或可移植的。"