Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Basic knowledge of c language
Basic knowledge of c language
Structural understanding of 1 C language program

Through a simple example of C program, this paper introduces the basic composition, format and good writing style of C language, so that readers can have a preliminary understanding of C language.

Example 1 C program for calculating the sum of two integers:

1, any C language program must include the following format:

This is the basic structure of C language, and any program must include this institution. Nothing can be written in parentheses, so the program will not execute any results.

2.main()- called "main function" in C language. A C program has only one main function, and any C program always starts with the main function. Parentheses after the main function cannot be omitted.

3. The content enclosed in braces is called the function body of the main function, which is what the computer will execute.

4. At {? } Every sentence in it is followed by a semicolon (; ), in C language, we call a sentence ending with a semicolon a statement in C language, and a semicolon is a sign of the end of the statement.

5、printf("a=%d,b=%d,sum=%d\n ",a,b,sum); -By executing the screen output function provided by the C language system for us to use directly, users can see the running results. After the program runs, the following results will be displayed on the display screen:

6, # including

7. The part in the program that starts with/* and ends with */indicates the comment part of the program. You can add comments anywhere in the program to improve the readability of the program, but the computer completely ignores the comment part when executing the content of the main function. In other words, the computer does not exist as the comment part in the main function.

? Generation process of 2 C program

C program is to compile source files to generate target files, and then connect them to generate executable files, as shown in the figure.

? compilation process

? 3 identifier

When writing a program, you must name functions, variables, etc. This name is called an identifier. The naming rules of identifiers in C language are as follows:

Identifiers cannot be the same as keywords with special meanings in the program, user-compiled function names and C language library functions. In the program, various identifiers should not be repeated as much as possible in order to distinguish them. When selecting identifiers such as variable names, we should pay attention to "knowing the meaning by name".

Identifiers are divided into the following three categories:

1, keywords

3. User identifier

Identifiers defined by users themselves according to their needs are called user identifiers. No matter how to customize the identifier, it must conform to the three naming rules of the identifier.

4 constant

In the running of a program, the quantity whose value cannot be changed is called a constant. There are five types of constants: integer constants, real constants, character constants, string constants and symbolic constants.

(a) numerical conversion

Four manifestations of numbers:

In computers, numbers are represented and stored in binary form. Ordinary decimal numbers input by users can only be stored in the computer after being converted into binary by the computer. Similarly, the results of computer operations are binary, usually converted into decimal numbers, and then output to users for reading. This conversion is usually realized automatically by computers.

(1) Converts decimal to binary, octal and hexadecimal.

Division: Divide the decimal by 2, record the remainder, and continue to divide the quotient by 2 until the quotient is 0. Then, the remainder obtained each time is arranged in reverse order from back to front, and the order of digits of the remainder is the binary number corresponding to the decimal number. The conversion method between octal and hexadecimal is the same as above.

Example: Decimal number 13 is converted into binary number 1 10 1, into octal number 0 15, and into hexadecimal number D. 。

(2) Convert binary, octal and hexadecimal into decimal

Product sum: each bit of binary from low bit to high bit (low bit on the right and high bit on the left) is multiplied by 20,265,438+0,22 respectively. . . . , and then sum these products.

For example, (110/) 2 = (13)10 (317) 8 = (207)10 (23e)

(3) the conversion between binary number, octal number and hexadecimal number

(2) Integer constant

There are three forms of integer constants: decimal integer constants, octal integer constants and hexadecimal integer constants.

The writing method is as follows:

(3) Real constant

There are two representations of real constants: decimal form and exponential form.

(4) Character constant

(2) Enclosed in a pair of single quotation marks, starting with a backslash \, followed by several numbers or letters, such as' \n', where' \ n' means escape, followed by different characters means different meanings. This character constant is called escape character. As shown in the figure.

(5) String constants

In C language, a sequence of several characters enclosed in double quotation marks is a string constant.

For example: Ni Hao? "Happy" and so on.

(6) Symbolic constant

Example: C program for calculating the area of a circle.

Description:

Five variables

A variable is an amount whose value can be changed. A variable should have a variable name and occupy a certain storage unit in memory, and the variable value is stored in the storage unit. Different types of variables have different storage unit sizes, so variables must be defined before use.

(1) integer variable

Different compilation systems have different regulations on the number of digits and numerical range of the above four kinds of integer data.

Type specifier

Description:

(b) Real variables

In C language, real variables are divided into single-precision (float) and double-precision (double). For example:

In vc, floating-point data accounts for 4 bytes (32 bits) and double-precision data accounts for 8 bytes. Single-precision real numbers provide 7 significant digits, and double-precision real numbers provide 15 ~ 16 significant digits. For real constants, there is no difference between floating-point type and double-precision type. Real constants can be assigned to floating-point or double-precision variables, but the variables will intercept the corresponding significant digits in the real constants according to their types.

Note: Real variables can only store real values, not integer variables or integer values.

(3) Personality variables

Character variables are used to store character constants, and their definitions are as follows:

Charles? Variable name;

Among them, the keyword char defines the character data type and occupies one byte of storage unit.

When a character is assigned to a character variable, the character itself is not stored in the memory, but the ASCII code corresponding to the character is stored in the memory unit. For example, the ASCII code of the character' a' is 65, and the storage form in the memory is as follows: 0 100000 1.

Because characters are stored in memory in the form of ASCII code, and their storage forms are similar to integers, character data and integer data can be used in C language. A character can be output as a character or an integer, and the character data can also be arithmetically operated, which is equivalent to operating their ASCII codes.

? Automatic conversion and forced conversion of type 6

When the data types in the same expression are different, the compiler will automatically convert them to the same type before calculation. The conversion priority is:

That is, the type of the left level "low" is converted to the right side. Specifically, if the data with the highest priority in the expression is of double type, all other data in the expression will be converted to double type, and the calculation result will also be of double type; If the data with the highest priority in the expression is floating-point, all other data in the expression are converted to floating-point, and the calculation result is also floating-point.

When doing assignment operation, if the types on the left and right sides of the assignment number are different, convert the type on the right side of the assignment number into the type on the left; When the type on the right is higher than the type on the left, the data on the right is intercepted during the conversion.

In addition to automatic conversion, there is also forced conversion, which is expressed as:

Discussion: When the value of A is 3.4 and the value of B is 2.7, what are the values of (int)(a+b) and (int)a+b respectively?

7 ? Understanding of c operator

C language has a wide range of operators, which can be divided into the following categories:

1, arithmetic operator: used for various numerical operations. Including addition (+), subtraction (-), multiplication (*), division (/), remainder (%), self-increasing (++) and self-decreasing (-) * *.

2. Assignment operator: used for assignment operation, which is divided into simple assignment (=), compound arithmetic assignment (+=,-=, * =,/=,% =) and compound bit operation assignment (&; =,|=,^=,>; & gt=,<& lt=) There are three kinds of * * * eleven kinds.

3. Comma operator: used to combine several expressions into one expression (,).

4. Relational operator: used for comparison operation. Include greater than (>), less than (=), less than or equal to (

5. Logical operator: used for logical operation. Including using (&; &), or (||), not (! ) three kinds.

6. Conditional Operator: This is a three-eye operator used for conditional evaluation (? :)。

7. Bitwise operation operator: operate the quantity involved in the operation according to the binary bit. Includes a bit sum (&; ), bitwise OR (|), bitwise NOT (~), bitwise XOR () and left shift (>).

8. Pointer operator: used to extract content (*) and address (&; ) two operations.

9. Byte number operator: used to calculate the size of data type.

10, special operators: bracket (), subscript [], member (→,. ), etc.

In addition, according to the number of objects involved in the operation, C language operators can be divided into: monocular operators (such as! ), binocular operators (such as+,-), tricolor operators (such as).

First,? Basic arithmetic operator

(1)+ (addition operator or positive operator, such as 2+5).

(2)- (subtraction operator or negative operator, such as 4-2).

(3)* (multiplication operator, such as 3*8).

(4)/ (division operator, such as 1 1/5).

The operation of/is divided into two situations:

(5)% (modular operator or remainder operator, both sides of% should be integer data, for example, the value of 9%7 is 2).

It should be noted that when the operand is negative, the result will be different with different compilers. In vc, the sign of the result is the same as the dividend. For example, the value of 13%-2 is 1, while the value of-15%2 is-1.

Second,? Priority and combination of arithmetic expressions and operators

Arithmetic expression is an expression that connects operands (also called operands) with arithmetic operators and brackets and conforms to the grammatical rules of C language. Operands include functions, constants and variables.

In computer language, the evaluation rules of arithmetic expressions are similar to those of four operations in mathematics, and their operation rules and requirements are as follows.

(1) In an arithmetic expression, you can use multiple parentheses, but the parentheses must appear in pairs. The value of each expression begins with parentheses and is calculated from the inside out.

(2) In the arithmetic expression, operators with different priorities can operate from high to low. If the operators in the expression have the same priority, they can operate according to the combination direction of the operators.

(3) If the types of operands on both sides of an operator are different, use automatic conversion or forced type conversion to make them have the same type, and then perform the operation.

Third,? Self-increasing and self-decreasing operator

Function: Increase or decrease the value of a variable by 1.

(1) Only variables can use the self-increasing operator (++) and the self-decreasing operator (-), but constants or expressions cannot be used. For example, 10++ or (x+y)++ are illegal.

(2) The combination direction of++and-is "from right to left", such as -i++, with the negative sign operator on the left and the self-increasing operator on the right. Both the minus sign operation and the self-increasing operation are "from right to left", which is equivalent to -(i++).

Self-increasing (decreasing) operators are often used in loop statements and pointers. Candidates should make clear the differences between "i++" and "++i" and "I-" and "-I", especially the values of expressions and variables.

For example: the initial value of the variable n is 2, then

Example: There are the following procedures.

The output result after the program runs is _ _ _ _ _

A. 12? 35 ? 13 ? 35 ? 14 ? 36 B. 12? 35 ? 14 ? 35 ? 14 ? 36

C. 12? 35 ? 14 ? 36 ? 14 ? 36 D. 12? 35 ? 14 ? 35 ? 14 ? 35

Analysis: In the self-increasing and self-decreasing operation, it is necessary to distinguish the value of an expression from the value of a variable. After the expression operation, the variables will increase and decrease, and the changes of expressions and variables are as follows:

? 9 assignment operators and assignment expressions

First, assignment operators and assignment expressions

The assignment symbol "=" is an assignment operator, which is used to assign a data to a variable or the value of one variable to another variable. An expression composed of assignment operators is called an assignment expression. The general form is:

Variable name = expression

You can assign a variable multiple times in the program. Every time an assignment is made, the data in the corresponding storage unit is updated once, and the current data in the memory is the data assigned for the last time.

Description:

A. If the operand types on both sides of the assignment number are inconsistent, the system will automatically perform type conversion. The conversion rule is to convert the type of the value of the expression to the right of the assignment number to the type of the variable to the left of the assignment number.

B. You can reassign the value of the copied expression to a variable to form a continuous assignment.

Second, the compound assignment operator

Adding other operators before the assignment operator can form a compound assignment operator. Among them, the compound operators related to arithmetic operations are:+=,-=, * =,/=,% =.

There can be no space between two symbols, and the priority of compound assignment operator is the same as that of assignment operator. The expression n+= 1 is equivalent to n=n+ 1, which is used to add 1 to the variable n, and so on.

If you want to represent the value of a+=a-=a*a, the initial value of a is 12.

Steps:

10 ? Comma operators and comma expressions

In C language, comma can be used as a separator or as an operator-comma operator to connect several expressions, such as a=b+c, a=b*c and so on.

The general form is:

Expression 1, expression 2, expression 3, …, expression n

For example: x = 2, y = 3, z = 4.

The comma expression has a combination from left to right, that is, the expression 1 is solved first, and then the expression 2 is solved in turn until the value of the expression n is reached. The value of the expression n is the value of the entire comma expression. The value of the comma expression above is the value of the expression z=4. It should be noted that the comma operator is the lowest level of all operators.

Example: There are the following program segments:

The result of the program is: y=6, x=6.

Discussion: put y=(x=a+b+c);, (b+c); Change it to y=((x=a+b), b+c)?

1 1 relational operators and relational expressions

First,? Logical values in c language

There are only two logical values in C language: true and flase. Use nonzero for truth and zero for false. Therefore, for any expression, if its value is zero, it means false value, and if its value is non-zero, it means true value. As long as the value is not zero, whether it is a positive number, a negative number, an integer or a real number, it represents a true value. For example, the logical value -5 is true.

Second,? Relational operators and their priorities

C language provides six relational operators, as shown in the table.

No spaces can be added between two-character operators, and relational operators are binocular operators.

(1) Binding: from left to right.

(2) Priority: the first four relational operators (

Third,? relational expression

Expressions connected by relational operators are called relational expressions.

For example, a>b, (a=7)>(b= 10) and so on are all legal relationship expressions.

The relational expression has two results: 0 and 1. Where 0 means false and 1 means true. If the relational expression holds, the value is true; If the relational expression does not contain, the value is false.

Example: If the value of variable A is 5 and the value of variable B is 6, then the relational expression A >;; The value of b is false, that is, 0. Sum relational expression

The value of (a= 13)>(b= 10) is true, that is, 1.

When the types of values on both sides of the relational operator are inconsistent, if one side is an integer and the other side is a real number type, the system will automatically convert the integer into a real number type, and then compare them.

? 12 logical operators and logical expressions

First,? Logical operators and their priorities

C language provides three logical operators, as shown in the following table.

Second,? logic expression

"& and" || "have two operands, so they are both binocular operators, and! There is only one operand, so it is a monocular operator. Examples of logical operations are as follows:

( 1)a & amp; & ampb: When &; & When both sides are true, the expression A &;; The value of & ampb is true.

It is worth noting that in mathematics, the relation 0

(2)a||b: When one of the two sides of || is true, the value of expression a||b is true.

(3)! A:? If a is true, then! A is false, and vice versa. For example! The value of -5 is 0.

In C language, it was written by&; & or |||, under certain circumstances, "short circuit" will occur.

( 1)x & amp; & AMPY & AMPZ, only when X is true (non-zero), you need to judge the value of Y; Only when x and y are true, you need to judge the value of z; As long as x is false, there is no need to distinguish between y and z, and the value of the whole expression is 0. Formula: "False must be false."

Example: (! 5 = = 1); & amp(++i==0)? (! The value of the expression 5== 1) is 0, so the expression (++i==0) is skipped when the computer is running. 5 = = 1); The value of the&(++I = = 0) expression is 0.

(2) x ||| y || z, as long as the value of x is true (non-zero), there is no need to judge the values of y and z, and the value of the whole expression is 1. Only when the value of X is false, you need to judge the value of Y. Only when the values of X and Y are false, you need to judge the value of Z. The formula is "One truth must be true".

13 bit operation

First,? Position operator

In a computer, data is stored in the form of binary numbers, and bit operation refers to the operation of binary bits in a storage unit. C language provides six bit operators.

Second, bit operation

Bit operator &; ? | ~ & lt& lt? & gt& gt? ∧ The order from high priority to low priority is:

Among the bit operators, the negation operation "~" has the highest priority, while the left shift and right shift are&,ranking second, and the next order is bitwise AND "&;" , bitwise XOR "∧" and bitwise OR "|". What's the order? & lt& lt? & gt& gt? & amp? ∧ ? | ? .

Example 1 left shift operator "

Example 2 The right shift operator "> > is a binocular operator. Its function is to shift all the bits of the left operand of "> > by several bits to the right," >; > the number on the right specifies the number of digits to move.

It should be noted that for signed numbers, when moving to the right, the sign bit will move with it. When it is a positive number, most significant bit is 0, and when it is a negative number, the sign bit is 1, and whether most significant bit is 0 or 1 depends on the compilation system.

Example 3 Let the binary number A be 0010110/. If a∧b is operated by XOR, the upper four bits of a are inverted and the lower four bits are unchanged, then the binary number b is.

Analysis: XOR is often used to flip a specific bit, as long as the bit to be flipped is XOR with 1, because the bit with the value of 1 in the original number is XOR with 1 to get 0, and the bit with the value of 0 in the original number is XOR with 1 to get 1. While the bits XOR with 0 will keep the original value. XOR can also be used to exchange two values without temporary variables.

So the answer is:11110000.