Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - What's the difference between scanf_s () and scanf?
What's the difference between scanf_s () and scanf?
1, usage difference

Scanf () does not check the input boundary, which may lead to data overflow.

Scanf_s () performs a boundary check.

2. Significance

Scanf means inputting data in the specified format from the keyboard. Such as: scanf("%d ",x); Refers to inputting an int type (integer type) data from the keyboard to X; scanf("%f ",x); Refers to the input of floating-point (real) data from keyboard to X;

The corresponding output is: printf? Output data according to the specified format; Such as printf("%d ",x); ? Refers to outputting the value in x based on integer data.

Because the function with "_s" suffix is to make the original function more secure, pass in a size value related to the parameter, avoid quoting non-existent elements, and prevent hackers from using the insecurity (vulnerability) of the original function to hack the system.

3. The parameters of scanf _ s () are different from those of scanf ().

For example, scanf ("%s ",&; Name, n), the integer n is the size of the type name, and if name is an array, then n is the size of the array.

Extended data

Precautions:

(1) In the later version of Visual Studio compiler, scanf is considered unsafe and abandoned, so scanf_s should be used instead of scanf.

(2) For a string array or a string pointer variable, because the array name can be converted into an array and the pointer variable name itself is an address, there is no need to add "&"before them when using the scanf () function. Operator.

(3) An integer can be added between the "%"format specifiers in the formatted string to indicate the maximum number of digits in any read operation.

(There is no precision control like printf in the scanf function.

Such as: scanf ("%5.2f ",&; a); It's illegal. You cannot attempt to enter a real number with two decimal places using this statement.

(A variable address is required in scanf, and if a variable name is given, an error will occur.

Such as scanf("%d ",a); Is illegal and should be changed to scanf ("%d ",&; a); It is legal.

(6) When inputting multiple numerical data, if there are no unformatted characters in the format control string as the interval between the input data, spaces, tabs or carriage returns can be used as the interval.

When the C compiler encounters spaces, tabs, carriage returns or illegal data (for example, when "12A" is entered in "%d", A is illegal data), the data is considered to be over.

(7) When inputting character data (%c), if there are no unformatted characters in the format control string, all the input characters are considered as valid characters.