C language data type, variable input and output conversion

1. The scanf standard function can get numbers from the keyboard and record them into the storage area.
In order to use this standard function, you need to include the stdio.h header file.
In the scanf function call statement, you should use the address of the storage area to indicate the storage area. Example: scanf("%d",&num);
Placeholders are used in double quotes to indicate the type of storage area.
In the scanf function call, try not to write content that is not a placeholder.

 

2. If the format of the user input is different from the format required by the program, then the program cannot get the number.
Multiple numbers can be obtained in one scanf function call statement. Example: scanf("%d%d",&num,&num1);


3. A byte can be divided into eight segments, and each segment can be used to record a 0 or 1.
If you want to record a number into a byte, you must first split it into eight 0 or 1
and use a group of 0 or 1 to represent the number. The method is called binary.
Any number can be expressed in decimal or binary.
4. Computers can only store numbers in binary.
Each position of a binary number has a number, the number of the rightmost position is 0, and it increases in turn to the left.
The 1 in each digit in a binary number represents a single number, and this number is 2 to the power of the digit number.
In a binary number, if the content of two adjacent digits is the same, the content of the left digit is twice that of the right digit.
Non-negative numbers represented in binary conform to the above rules.
When adding one to a binary number, the number of consecutive 1s starting at position 0 should be changed to 0, and the rightmost 0 should be changed to 1. The
result of dividing any binary number by 2 and retaining the integer part is equivalent to removing the rightmost binary digit. Content.
When converting a non-negative number represented in binary to decimal, you only need to convert the content of each digit separately and then sum the converted results.

example:

0000 0011 = 2 to the 1st power + 2 to the 0th power
          = 2 + 1
          = 3
0101 1100 = 2 to the 6th power + 2 to the 4th power + 2 to the 3rd power + 2 to the 2nd power
          = 64 + 16 + 8 + 4
          = 92

5. The method of converting non-negative numbers expressed in decimal to binary:

12 *********0
6 *****0
3 *****1
1 ****1
0 ****
The conversion result is 00001100

Continuously divide the original number by 2 and keep the integer part to get a set of numbers, divide each number by 2 and take the remainder to get the content of a digit, and write all the digits in reverse order from the back to the front to get the conversion the result of.

6. Negative numbers cannot be directly converted between binary and decimal, and the opposite number must be used.

The conversion process is divided into three steps, first calculate the opposite number, then convert the opposite number, and finally calculate the opposite number again.

Change the content of each digit of the binary number into the opposite number and then add one to get the binary of the opposite number.

Example of converting decimal negative numbers to binary negative numbers:
-14
14
0000 1110
1111 0001 + 1 = 1111 0010 (binary of -14)

7. The leftmost digit in a signed binary number is called the sign bit, and its content can be used to determine whether the number is negative or non-negative.
A sign bit content of 0 indicates that the number is non-negative. A sign bit content of 1 indicates that the number is negative.
Example of converting binary negative numbers to decimal:
1100 1011
0011 0100
0011 0100 + 1 = 0011 0101
0011 0101 = 2 to the 5th power + 2 to the 4th power + 2 to the 2nd power + 2 to the 0th power
          = 32 + 16 + 4 + 1
          =
53-53

8. Group the digits of binary numbers into groups of three from right to left. , each group is replaced with a number between 0-7. The result of this substitution is called the octal representation of the number.

Example:
0110 1010
01 101 010
1 5 2
152 (octal)
You can directly use octal to represent numbers in the program, and such numbers must start with 0.
You can use %o as a placeholder in the printf function call statement to display the octal representation of the number on the screen.

9. Divide the binary digits into groups of four digits from right to left, and replace each group with a character (replace numbers between 10 and 15 with letters between a and f). The result of this substitution is called the hexadecimal representation of the number.

Example:
1100 1011
12 13
c b
cb (hexadecimal)

10. You can use hexadecimal to represent numbers in the program, and the numbers must start with 0x. You can use %x or %X
as a placeholder in the printf function call statement to display the number on the screen. When lowercase %x is used as a placeholder, all letters in the number are lowercase, and when uppercase %X is used as a placeholder, all letters in the number are uppercase .


11. Operators represent calculation rules for numbers.
Operators can be divided into unary operators, binary operators and ternary operators according to the number of numbers that the operators need to match.

12. In C language, +, -, * and / are used to represent the four arithmetic operations of addition, subtraction, multiplication and division.
If the two numbers involved in the division calculation are both integers, only the integer part is kept in the calculation result.

13. In the C language, % is used to represent the remainder operation

14. The assignment operator is represented by =
This operator can record a number into a storage area.
The assignment statement can be used as a number, and this number is the number in the storage area on the left after the assignment is completed.
Multiple assignment operators can be used in a statement, in which case the rightmost assignment operator is evaluated first.

15. Most binocular operators can be combined with assignment operators and become compound assignment operators, for example: +=, /=, etc.
Compound assignment operators also require that the content on the left can represent a storage area, and the content on the right can represent a number.
The compound assignment operator records the calculation result of the binary operator into the storage area on the left.
Compound assignment operators have the same low precedence as assignment operators. Example: 5*=2+3; the result is 25, first calculate 2+3=5, then calculate 5*=5, the final result is 25

16. The self-increment operator (++) and the self-decrement operator (--) are unary operators.
These two operators must be used in conjunction with the storage area, and they can add or subtract one to the content of the storage area.
They all have two usage methods, one is pre-operation (the operator is written in front of the storage area), and the other is post-operation (the operator is written behind the storage area).

Expressions written using these two operators can be used as numbers. When the former operation is used as a number, it is the modified number, and when the post-operation is used as a number, it is the number before modification.
Do not increment or decrement the same variable multiple times in one statement.

17. Logical operators are used to write logical expressions.
The result of a logical expression can only be a Boolean value.

18.! Is a unary logical operator, it is used to calculate the opposite value of a Boolean value.
This operator should be written in front of a Boolean value when used.

Binary logical operators include == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to)

Example: 3 < 7 < 5 results in 1 (true)
because 3 < 7 is calculated first and the result is 1 (true)
and then 1 < 5 is calculated so the result is 1 (true)

19. An expression that contains at most one binocular logical operator is called a simple logical expression. The result of this logical expression in mathematics must be the same as that in C language.

20. When writing a logical expression in C language, if the logical expression contains multiple binocular logical operators, it must be split into multiple simple logical expressions, and then combined.

21. You can use logical AND (&&) and logical OR (||) to combine two logical expressions into one.

If the result of one of the two logical expressions is true, the result is true after joining with or (||). (Logical or one true is true)
If the result of one of the two logical expressions is false, the result is false after connecting with (&&). (Logical and one is false)
and (&&) and or (||) both have short-circuit characteristics (if the result of the previous logical expression can determine the result of the combined logical expression, the result of the latter expression will not be calculated It turned out).

Guess you like

Origin blog.csdn.net/weixin_44954230/article/details/127225032