First, C language programming problems

Max_value (int x, int y) a function header contains three important pieces of information: the name of the function, the return value type of the function, and the argument list of the function. In this example, the function name is max_value, the return type of the function is int, and the argument list is (int x, int y). These three pieces of information are the interfaces to this function. 2, function body: {int z; z = x>y ? x : y; return z; } b) int main(int argc, char const *argv[]) return int main(int argc, char const *argv[]) return int mainCopy the code

Second, operators

A) bit operators:

1. ~ : reverse by bit

2, & : bit sum for two variables: 0 is 0, both 1 is 1

3, : carry out bit and or for two variables: 1 is 1, both 0 is 0

4, ^ different or: the same is 0, different is 1

5 Move left and move right:

First, if the sign bit complements 0 for a right shift, it is called a logical right shift. Second, if a right shift complements a copy of the original sign bit, it is called an arithmetic right shift.Copy the code

The operation efficiency of left shift and right shift is much higher than multiplication and division, left shift is equivalent to multiplication and right shift is equivalent to division.

Shift to the left

To move to the left is to move all the bits of a number several to the left, using the << operator in C.

For example, int I = 1; i = i << 2; // Shift the value of I to the left by 2 bits, i.e., base 1 is 000... 0001(the number of zeros before 1 is related to the number of ints,32 bit machines, GCC has 31 zeros), after moving 2 bits to the left, it becomes 000... 0100 is 4 in base 10, so moving one bit to the left equals multiplying by 2, so moving n bits to the left equals multiplying by 2 to the n. One thing to note is how the left-most sign bit of an int is shifted out. Int I = 0x40000000; int I = 0x40000000; // 40000000 in hexadecimal, 01000000 in hexadecimal... 0000 i = i << 1; So, I moves one bit to the left and becomes 0x80000000, which is 100000 in base 2... 0000, the symbol bit is set to 1, and all other bits are set to 0. This becomes the smallest value that can be represented by an int. The 32-bit int is -2147483648 and overflows. What happens if I move I to the left one more place? In C language, the method of discarding the highest bit is adopted. After discarding 1, the value of I becomes 0. A special case of left shift is when the left shift exceeds the maximum number of digits of the numeric type, the compiler uses the left shift to remove the maximum number of digits of the type, and then shifts it by remainder, for example: int I = 1, j = 0x80000000; // set int to 32 bits I = I << 33; // 33%32 = 1, I = 2, j = j << 33; // 33%32 = 1 left shift 1 bit,j becomes 0, the highest bit is discarded So essentially I and j are moving one bit, which is 33% of the remainder after 32. Under GCC this rule is true, and it is not clear whether other compilers are the same. So to move to the left is: discard the highest digit, and 0 fills the lowest digitCopy the code

Moves to the right

Again right move, understand the truth of left move, then right move is easier to understand.

The concept of a shift to the right is the opposite of a shift to the left, in that it moves several bits to the right. The operator is >>. A shift to the right treats the sign bit differently than a shift to the left. i = i >> 1; The value of // I will not change to 0x40000000, but will change to 0xC0000000. Also, when the number of bits moved exceeds the length of the type, the remainder is taken, and then the remainder is moved. A negative number of 10100110 >>5(assuming an 8-bit word length) yields 11111101Copy the code

Anyway, in C, a left shift is a logical/arithmetic left shift (both are exactly the same), and a right shift is an arithmetic right shift, leaving the sign bit unchanged. In practice, it is possible to multiply/divide quickly by left/right shifts depending on the situation, which is much more efficient than the loop.

Example :C language left shift << means multiplied by 2, right shift >> means divided by 2, this is caused by the working principle of the computer! But if it's 7, the binary number is 0111, so if you move it one bit to the right you get 3.5, but if you move it to the right you get 0011, which is 3. It's different. How do you explain that? The two operands of the shift operator must be integers. The value of the entire shift expression is of integer type, and the left shift operator does not operate symmetrically with the right shift operator. Moving 0111 to the right removes the last 1 and adds a 0 to the left, giving 0011, which in decimal form is 3, which is correct. It's not the same thing as dividing by 2.Copy the code

B) Negative storage

I. Big-endian sequence: low address stores high byte order

Ii. Little endian: low address storage low byte order

C) When negative numbers are stored, the binary number is negated and then complemented.

D) trinary operator:? :

 

Loop control statement

w

1. While loop

When you want to execute something looping, a while loop can do this. The standard syntax is as follows

Where expression can be any expression (in C, every expression has a definite value), the while statement uses the value of expression to decide whether to execute the following statement (also called the loop body). If the value of expression is false, the statement is skipped. If the value of expression is true, the statement is executed. This loop continues until its value is false (if expression is always true, it is called an infinite loop) and is executed as follows

From the figure, we can clearly see the execution logic of the while loop. The execution flow runs from top to bottom. When encountering the while loop, expression P is first evaluated. So the while loop is also called”Inlet condition cycle“, because it tests the loop condition (the value of the expression p) before executing the loop body,If the expression p is false to begin with, the body of the loop will not be executed once.

2, the do… The while loop

The execution logic is: no matter three seven twenty-oneExecute statement once againIf true, statement is executed again and the loop continues until expression is false. In addition, special attention should be paid toThe do… The while loop must end with a semicolon(A while loop does not have a semicolon after the while; if it does, the empty statement formed by the semicolon becomes the body of the loop.) The following is a flow chart of its executionThe do… While executes the body statement A and evaluates the value of the expression P to decide whether to continue the loop.

3. For loop

The for loop is one of the more flexible loop constructs, occurring in the Linux kernel about as often as the while loop and do… Four or five times the while loop, and here is its general form:

for(initialize; test; update)

statement

The statement block can be either a single statement or a compound statement enclosed in curly braces. The parentheses following the keyword for contain three expressions whose names indicate their general use as a for loop structure, namely:

The first expression is typically used to initialize a loop control variable

The second expression is commonly used as a circular test condition

The third expression is generally used to update loop control variables, but syntactically they can be arbitrary statements.

The execution logic of the for loop structure:

1. Execute an initialize statement if there is one, then skip to step 2, or skip to step 2 if there is none. 2. Skip to step 3 if there is a test statement and its value is true or there is no test statement. Otherwise, skip to step 3 if there is a test statement and its value is false. 3. Execute the loop body statement. Step 4 is complete. 4. Execute if there is an UPDATE statement and skip to step 2, or skip to step 2 if there is no UPDATE statement. As you can see from the statement above, three expressions in the for loop can be omitted, but the semicolons between them cannot be omittedCopy the code

4. Branch control

Similar to loop control, branch control is the most basic program control syntax of C language. It is used to make certain statements executed or not executed under certain conditions, which greatly improves the flexibility of program execution.

A, two branches if else if

The so-called two-way branch refers to an either-or logical relationship, as shown in the figure below

An either-or branch relationship implements the logical code as follows:

If (expression) // If expression is true, execute statements 1 {statements 1} else // If expression is false, Execute statements 2 {statements 2}Copy the code

In the above example, the else statement represents all possible cases except when expression is true. This statement can be omitted, but if there is an else statement, it must be followed by an if

B. Multi-channel branch switch

Syntax for switch statements:

1. The expression in the switch(expression) statement can be a variable, an expression, or even a function call, but the result must be an integer. 2. In case constant, constant must be an integer constant, neither const, but can be a char constant, such as 'w'. 3. Once the value of expression is determined to be equal to the constant value of a case statement, it is executed from the case statement down until the break statement or the end of the switch structure is encountered. 4. When there is no constant in any case statement equal to expression in the switch statement, execute the default statement, but this statement is not required and can be omittedCopy the code

C. Direct jump

Goto is most commonly used in error processing. Small white is not recommended, but Daniel likes to use it