- Memory Analysis of variables
- Bytes and Addresses
To better understand the storage details of variables in memory, first recognize the "bytes" and "addresses" in memory.
- Memory in "bytes"
0x is the hexadecimal, not too tangled, can understand the numbers between the big who is small on the line
- The bytes occupied by different types are not the same, and the larger the data, the more the number of bytes required
- Storage of variables
- The number of bytes consumed is related to the type, but also to the compiler environment
- Variable instance
int B = ten;
int a = ;
- Memory is addressed by large to small, giving precedence to variables with large bytes of memory addresses. B has a larger memory address than a
- Each variable has an address: the address of the first byte is the address of the variable.
- To view memory addresses:
int A;
printf ("A 's address is:%p\n", &a);
- Attention
Do not attempt to use the value of a variable before it is initialized
int A;
printf ("A 's value is:%d\n", a);
The above wording is not recommended .
- scanf function
- Brief introduction
This is also a function declared in stdio.h , so #include <stdio.h> must be added before use . When calling the scanf function, you need to pass in the address of the variable as a parameter, andthescanf function waits for the standard input device (such as the keyboard) to enter data. and assigns the input data to the variable that corresponds to the address.
- Simple usage
int age ;
scanf("%d", &age);
- scanffunction when, it waits for the user's keyboard input and does not execute the code backwards. scanfThe first1A parameter is"%d", stating that the user is required toTenEnter an integer in the form of a binary. It is important to note thatThe First 2 arguments of scanf are not the age variable , but the address &ageof the age variable, & is an address operator in C that can be used to get the address of a variable
- After the input is complete, hit the ENTER key to tell the scanf function that we have already entered , andthescanf function assigns the input value to the Age Variable
- Other usage
- Use the scanf function to receive 3 numeric values , separated by an underscore between each value
scanf ("%d-%d-%d", &a, &b, &c);
3 %d is separated by an underscore- so we have to add an underscore after each integer input -suchas input, or when assigning a value to the variable will be a problem
- Note : The delimiter between the values is arbitrary , not necessarily with an underscore --It can be a comma, a space, an asterisk *, a pound sign # , and so on , even the English alphabet.
// comma ,
scanf ("%d,%d,%d", &a, &b, &c); // Input Format: 10,14,20
// Well number #
scanf ("%d#%d#%d", &a, &b, &c); // Input Format: 10#14#20
// Letters x
scanf ("%dx%dx%d", &a, &b, &c); // Input Format: 10x14x20
- Receive 3 values with the scanf function , separated by a space between each value
scanf ("%d%d%d", &a, &b, &c);
3 %d is separated by a space , we must enter a delimiter after each input integer, the delimiter can be a space ,tab, Enter
- Attention
Do not include \ n in the first parameter of scanf, such as scanf ("%d\n", &a); This causes the SCANF function to not end
- Exercises
Prompts the user to enter two integer n, and then calculates and outputs two integers and
Content Source: Preach intelligence podcast Li Mingjie teacher Content
C Language section fifth scanf function