C language---scanf function

Knowledge point 1: Scanf function usage formula

           1) scanf is a variable parameter function;

           2) The first parameter of scanf is a string;

scanf("%hhd %hd %d %ld %f %lf",&a,&b,&c,&d,&e,&n);

           3) The first parameter of scanf is the matching character and conversion specification;

scanf("%hhd %hd %d %ld %f %lf",&a,&b,&c,&d,&e,&n);\\算引号里面是需要输入的字符串以及转换规范

 #Please note: When inputting, you need to input it in the form of the first string, otherwise the correct result will not be obtained;

Example 1:

        The first string of scanf is "%hhd %hd %d %ld %f %lf", and each conversion specification is separated by spaces. Then the input needs to be separated by spaces, such as 1 2 3 4 5.6 7.8  ; 

Example 2:

        The first string of scanf is "%hhd,%hd,%d,%ld,%f,%lf", and each conversion specification uses, split. Then the input needs to be separated by commas, such as 1,2,3,4,5.6,7.8;

Example 3:

       The first string of scanf is "%hhd+%hd-%dx%ld/%f~%lf", and the conversion specification uses +-x/~ to separate it. Then you need to enter 1+2-3x4/5.6~7.8 like this;   

           4) The subsequent parameters of scanf are the storage location of the data after the conversion is completed;

                 a) If scanf stores the converted binary into a basic variable, please add & before the variable name;

                 b) If scanf stores the string in a character array, there is no need to add & to the character array name;

           5) The writing method and quantity of the conversion specification need to correspond to the subsequent parameter types and quantities;

               a) hhd corresponds to char ;

               b) hd corresponds to short ;

               e) d corresponds to int ;

               d) ld corresponds to long ;

               e) f corresponds to float ;

               f) lf corresponds to double ;

Knowledge point 2: The use of scanf function

          1) Match the input string with the first parameter (first, the scanf function reads the input string. Then, scanf will match the input string with the string of the first parameter to find the input string One-to-one correspondence between substrings in and conversion specifications);

           2) Convert characters into binary according to the conversion specification (the conversion process starts after the substring matches the conversion specification. scanf will use different conversion methods to convert the substring into binary according to the conversion specification corresponding to the substring);

length indicator conversion specification Convert to some type of binary
hh d char
h d short
none d int
l d long
ll d long long
hh u unsigned char
h u unsigned short
none u unsigned int
l u unsigned long
ll u unsigned long long
none f float
l f double
none c ASCLL code table corresponding to characters
none s ASCII code corresponding to the character in the string

scanf("%hhd %hd %d %ld %f %lf",&a,&b,&c,&d,&e,&n);
子串 "1" 对应转换规范 "%hhd" ,将转换为char类型的二进制表示,1字节
子串 "2" 对应转换规范 "%hd" ,将转换为short类型的二进制表示,2字节
子串 "3" 对应转换规范 "%d" ,将转换为int类型的二进制表示,4字节
子串 "4" 对应转换规范 "%ld" ,将转换为long类型的二进制表示,4字节
子串 "5.6" 对应转换规范 "%f" ,将转换为float类型的二进制表示,4字节
子串 "7.8" 对应转换规范 "%lf" ,将转换为double类型的二进制表示,8字节

           3) Put the converted binary into a variable;

Knowledge point 3: Several types of error demonstrations

          1) The length is correct but the type is wrong;

#include <stdio.h>
int main()
{
long long ll;
scanf("%lf", &ll);
printf("%lld\n", ll);
printf("%f\n", ll);
return 0;
}

#We entered the string "123.45", which was matched by the conversion specification "%lf". Next, the string "123.456" will be converted into a binary representation of double type, 8 bytes. Finally, these 8 bytes are given to the long long type variable ll. Now the variable ll is an integer containing a double type binary. We use %d to print ll and the result must be wrong. So what if we use %f to print? %f will take 8 bytes of binary and convert it according to the double type binary rules. As a result, the correct result was obtained.

          2) The input string value is greater than the conversion type value range;

#include <stdio.h>
int main()
{
short s;
scanf("%hd", &s);
printf("%d\n", s);
return 0;
}

 

 #We entered the string "2147483467", which was matched by the conversion specification "%hd". Next, the string "2147483467" will be converted into a binary representation of type short, 2 bytes. The value range of the short type is -32767~32768, and 2147483467 cannot be installed with short. Therefore, correct results cannot be obtained.

        3) The variable cannot hold the conversion result;

#include <stdio.h>
int main()
{
short s;
scanf("%d", &s);
printf("%d\n", s);
return 0;
}

 #We entered the string "2147483467", which was matched by the conversion specification "%d". Next, the string "2147483467" will be converted to a binary representation of type int, 4 bytes. Finally, the converted 4-byte data is received by the short type variable s, and 2 bytes are lost. Therefore, correct results cannot be obtained.

        4) How to avoid mistakes;

#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
printf("%d\n", n);
return 0;
}

#We entered the string "2147483467", which was matched by the conversion specification "%d". Next, the string "2147483467" will be converted to a binary representation of type int, 4 bytes. Finally, the converted 4-byte data is received by the variable n of type int. Correct result.

@@ When using scanf, please note that the value of the input string must match the conversion specification and the variable type that receives the conversion result to get the correct result.

Knowledge point four: characters and strings

          1) Enter characters;

#include <stdio.h>
int main()
{
char c;
scanf("%c", &c);
printf("%d %c\n", c, c);
return 0;
}

#We input the string "A", which is matched by the conversion specification "%c". Next, the string "A" will be converted to a binary representation of type char (its decimal is 65), 1 byte. Finally, the converted 1-byte data is received by the char type variable c. When we print c using %d, the value 65 is output. When printing with %c, the character A is output.

#include <stdio.h>
int main()
{
char c;
scanf("%hhd", &c);
printf("%d %c\n", c, c);
return 0;
}

 

          2) Enter a string;

#include <stdio.h>
int main()
{
char str[10];
scanf("%s", str);
printf("%s", str);
return 0;
}

  # There is no string variable in C language, strings are stored in character arrays. Since the input string is stored in a character array, no & is added to the subsequent parameter str. We have not discussed arrays yet, so we will not continue to expand on this part for now.

Knowledge point 5: The scanf function is different from the printf function

          1) The subsequent parameters of the printf function do not need to be added with &, while scanf requires an address, so it is necessary to add & for the basic variables, but not for the array;

          2) The parameters of printf will be upgraded to int because variables smaller than int, and float will be upgraded to double. Therefore, the conversion specification d can be used for char, short, and int. The conversion specification f can be used for float and double. However, scanf directly sends the conversion result to the receiving variable, and the conversion specification must be strictly used.

Guess you like

Origin blog.csdn.net/m0_73463638/article/details/127133555