Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - C language problem: how to get an array of int type with scanf
C language problem: how to get an array of int type with scanf
introduction to the basic knowledge points of c language learning (3): scanf function

this section continues to introduce the basic knowledge points of c language.

scanf function: used to receive data input by users.

syntax: scanf ("formatting control character", address list);

address fetching characters are needed to fetch addresses:&; (shift+7)

For example: int num =; csanf("%d",& num); printf("num = %d \n",num);

Note: What type of data is required to be input before the formatting controller, then only the address of the corresponding type variable must be put in the address list. When you enter a value, you can specify the delimiter of the input value, and

you can also use carriage return by default.

features of p>scanf:

it is a blocking function.

for example: printf ("Please enter: \n");

int num ;

scanf("%d"& num);

printf ("Hehe, you entered %d",num);

enter multiple values at a time: int num; float num1; (you can enter n times, n= number of formatting controls)

scanf ("%d% f",&; num,& num1);

note: the order cannot be out of order; If you don't want to enter a line with spaces, you can specify other symbols, but you must follow this symbol when entering, otherwise you will get an exception (the variables behind you can't receive values); If you want to add text to scanf, you must also input it according to the format in scanf; When using scanf, it is best not to receive an arbitrary numeric type (integer, real type) first, but to receive a char type. If you have to receive a char type, you must add a space in front of the formatting controller of scanf. If scanf wants to receive numbers, no matter how many spaces you enter and enter, it will not stop you from entering; If scanf is to receive an int type, if you enter a decimal, only the integer part of the decimal will be intercepted. If you receive numbers and enter characters, the input will stop, but the value you entered will not be saved.

operation principle of scanf:

scanf is used to receive user's input, but the user's input is not directly given to variables, but there is a buffer. The steps are as follows:

1. When receiving input, I will first look in the buffer to see if there is an appropriate value in the buffer. If there is, I will stop inputting directly and assign the value in it to the variable. If not, I'll let you input until there is a suitable one.