The reason why the return value of scanf is ignored and its solution

Yesterday, when I used Visual Studio 2019 to write a C language program, I encountered the problem of scanf return value being ignored

1

Because I also just started using VS2019 to learn C language, I encountered this kind of problem for the first time, and I didn’t know what was going on. Then I went to Chrome to study it, and I knew the reason, and I found several scattered in every corner. The solution, I will summarize here .

problem causes:

In ANSI C, there is only scanf() and no scanf_s(), but scanf() does not check the boundary when reading, so it may cause memory leaks. So Microsoft provides scanf_s() in the VS compiler. If you want to continue to use scanf, an unsafe function, you can solve it by the following methods.

Method①:

Change scanf to: scanf_s

This is unique to the VS compiler. Since scanf() is considered unsafe, it defines a scanf_s().

3

Method ②:

The reason for the compiler error message As for the SDL check in VS, you only need to find it and close it.

Steps to close: Right-click "Project File"-"Properties"-"Configuration Properties"-"C/C++"-"General"-"SDL Check" to "No",
2
change as shown After that, you can compile it again.
2.5

Method ③:

Add code at the beginning of the C language program: #pragma warning(disable:4996)

//Turn off all

(or)

Add code at the beginning of the C language program: #pragma warning(once:4996)

//Only show one
5
15

Method ④:

Add code at the beginning of the C language program: #define _CRT_SECURE_NO_WARNINGS

//Ignore the security check
4
. Does it feel a little laborious to add such a string of codes every time? ? (When I first found this method, the first feeling was also like this)

Actually, it doesn’t have to be so troublesome,We only need to add the macro _CRT_SECURE_NO_WARNINGS to VS., The specific operation is shown in the figure below: do it
14
once, no more doing it later. I personally prefer the first method.

Guess you like

Origin blog.csdn.net/mxy3538/article/details/113181661