[Study Notes] [C Language] scanf function, learning notes scanf Function
1. Introduction
This is also a function declared in stdio. h. Therefore, you must add # include <stdio. h> before using it. When the scanf function is called, the address of the variable needs to be input as a parameter. The scanf function will wait for the standard input device (such as the keyboard) to input data and assign the input data to the corresponding variable of the address.
2. Simple usage
Int age;
Scanf ("% d", & age );
The scanf function waits for the user's keyboard input and does not run the code later. The first parameter of scanf is "% d", which indicates that the user must enter an integer in decimal format.
Note that the first parameter of scanf is not the age variable, but the address & age of the age variable. It is an address operator in the C language and can be used to obtain the address of the variable.
After entering the value, press enter to tell the scanf function that we have already entered the value. The scanf function will assign the value to the age variable.
3. Other usage
1> use the scanf function to receive three values. Each value is separated by a hyphen (-).
Scanf ("% d-% d", & a, & B, & c );
The three % d are separated by a hyphen (-). Therefore, after each integer is entered, A hyphen (-) must be added. For example, the separator between values is arbitrary, do not use a hyphen (-). It can be a comma, space, asterisk (*), a pound sign (#), or even an English letter.
// Comma,
Scanf ("% d, % d, % d", & a, & B, & c); // input format: 10, 14, 20
// Well Number #
Scanf ("% d # % d", & a, & B, & c); // input format: 10 #14 #20
// Letter x
Scanf ("% dx % d", & a, & B, & c); // input format: 10x14x20
1> use the scanf function to receive three values, separated by spaces.
Scanf ("% d", & a, & B, & c );
3% d are separated by spaces. After each integer is entered, a separator must be entered. The separator can be space, tab, or press Enter.
4. Note
Do not include \ n in the first parameter of scanf, for example, scanf ("% d \ n", & a); this will cause the scanf function to fail.
5. course code
1 # include <stdio. h> 2 3 int main () 4 {5 // define a variable to save the integer 6 int number entered by the user; 7 8 // the scanf function only accepts the variable address 9 // the scanf function is a blocking function, waiting for the user to input 10 // After the user input is complete, the user-input value is assigned to the number variable 11 // 12 scanf ("% d", & number) after the function call is completed ); 13 14 printf ("the value entered by the user is % d \ n", number); 15 16 return 0; 17}
1/* 2 prompt the user to enter two integers, calculate and output the two integers and 3 */4 5 # include <stdio. h> 6 7 int main () 8 {9 // 1. define two variables and save the integer 10 int num1, num2; 11 12 // 2. prompt the user to enter 1st integers 13 printf ("Please enter 1st integers: \ n"); 14 15 // 3. receive the 1st integers entered by the user 16 scanf ("% d", & num1); 17 18 // 4. prompt the user to enter 2nd integers 19 printf ("Please enter 2nd integers: \ n"); 20 21 // 5. receive 2nd integer 22 scanf ("% d", & num2); 23 24 // 6. calculate and, and output 25 int sum = num1 + num2; 26 printf ("% d + % d = % d \ n", num1, num2, sum ); 27 28 // printf ("num1 = % d, num2 = % d \ n", num1, num2); 29 return 0; 30}