C language entry: 05. scanf function, language entry 05. scanf
I. Memory Analysis of variables 1. bytes and address
To better understand the storage details of variables in the memory, first let's take a look at the "Byte" and "Address" in the memory ".
(1) memory in bytes"
0x indicates the hexadecimal format. You don't need to worry too much about it. You can understand who is big or small between these numbers.
(2) The Bytes occupied by different types are different. The larger the data, the more bytes are required.
2. Storage of Variables
(1) the number of bytes occupied is related to the type and the compiler environment.
(2) variable instances
int b = 10;int a = 20;
● From large to small addressing, the memory address is preferentially allocated to the variable in bytes with a large memory address. B's memory address is larger than a's
● Each variable has an address: the address of the first byte is the address of the variable.
(3) view the memory address:
Int a; printf ("the address of a is % p \ n", & );
(4) Note: The following statements are not recommended.
Do not use the value of a variable before initialization.
Int a; printf ("the value of a is % d \ n", );
Ii. scanf functions 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 the input is complete, press the Enter key to tell the scanf function that the input is complete. The scanf function assigns the input 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-%d", &a, &b, &c);
3% d are separated by a hyphen (-). Therefore, after each integer is entered, A hyphen (-) must be added. For example, enter 10-14-20, otherwise, problems may occur when assigning values to variables.
● Note: the delimiter between values is arbitrary. It may be a comma, space, asterisk (*), pound number (#), or even an English letter, rather than a hyphen (-).
// Comma, scanf ("% d, % d, % d", & a, & B, & c); // input format: 10, 14, 20 // Well # scanf ("% d # % d", & a, & B, & c); // input format: 10 #14 #20 // letter xscanf ("% dx % d", & a, & B, & c); // input format: 10x14x20
(2) Use the scanf function to receive three values, separated by spaces.
scanf("%d %d %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. Exercise
Prompt the user to enter two integers n, and then calculate and output the sum of the two integers