University c language must back to the basic knowledge _c language basic knowledge Daquan

For students who are just learning computer programming, no programming knowledge is very important, but it is not. The following small series for everyone to sort out the basic knowledge of the relevant university c language, I hope everyone likes it.

University c language must be based on basic knowledge

University c language must back to the basic knowledge _c language basic knowledge Daquan

for example:

Printf("-,123); The second part has three digits, which is larger than the specified two digits.

Printf("]",123 ); The second part has three digits, less than the specified five digits, and two spaces on the left. 123

Printf("f",1.25); The decimal number needs to be complemented by 6 digits, and there is no complement of 6 digits. The result is 1.250000

Printf("%5.3f",125 ); three decimal places, the entire five digits, the result is 1.250 (one decimal place)

Printf ("%3.1f", 1.25); one decimal place, the whole three, the result is 1.3 (to be rounded)

Section 3 data input

1, scanf ("a=%d, b=%d", &a, &b) exam super focus! It is important to remember to enter data in the terminal in the format of the first part. The core of the exam is: exactly the same. Enter a=12 and b=34 on the black screen to correctly assign 12 and 34 to a and b. It doesn't matter if it is different.

2, scanf ("%d,%d", x, y); this method is absolutely wrong, the second part of scanf must be the address! Scanf ("%d, %d", &x, &y); note that this can be written!

3, pay special attention to the pointer in the scanf for example: int x = 2; int * p = & x; scanf ("% d", x); error scanf ("% d", p); correct scanf ("% d" ,&p); Error scanf("%d", *p) error

4. Specify the length of the input (exam focus) Terminal input: 1234567 scanf ("-M%d", &x, &y, &z); x is 12, y is 3456, z is 7 Terminal input: 1 234567 due to 1 and 2 There are spaces in the middle, so only 1 bit is given to x scanf("-M%d", &x,&y,&z); x is 1, y is 2345, z is 67

5, characters and integers are close relatives: int x=97; printf ("%d", x); the result is 97 printf ("%c", x); the result is a

6, the difference between the input character and the integer (examination super focus)

Scanf ("%d", &x); enter 1 at this time, pay special attention to the integer 1

Scanf ("%c", &x); Enter 1 at this time, paying special attention to the character '1' ASCII as an integer 48.

Additional instructions:

1) The format of the scanf function:

Note that the second part of the function is an address such as &a, not a; scanf("%d%d%*d%d", &a,&b,&c); skips the third data entered.

2) putchar, getchar function test:

Char a = getchar() is no argument, get a character you type from the keyboard to the variable a. Putchar('y') outputs the character y to the screen.

3) How to achieve the exchange of values ​​in two variables x, y (required to back down)

It is not possible to use x=y, y=x; to use the intermediate variable t=x; x=y; y=t.

4) How to implement the procedure of retaining three decimal places and the fourth rounding (request to back down)

y=(int)(x*100+0.5)/100.0 This holds two digits and rounds the third digit

y=(int)(x*1000+0.5)/1000.0 This is reserved for three digits, rounded to the fourth digit

y=(int)(x*10000+0.5)/10000.0 This is reserved for four digits, rounded to the fifth digit

This has a general meaning, pay attention to x = (int) x This is to remove the fractional part.

third chapter

Pay special attention to the fact that in C language, non-zero is used to indicate logical truth, and 0 is used to indicate logical false.

The C language has a constructed type and no logical type.

Relational arithmetic symbols: Note the difference between the writing of =, == and =! (Exam focus)

If only the latter statement, to manage more than one, please use braces!

1) Relational expression:

a, the value of the expression can only be 1 (represented as true), or 0 (representing false).

For example, the relational expression of 9"8 is true, so the value of the expression 9"8 is 1.

For example, the relation expression of 7 "6 is false, so the value of the expression "7" is 0.

b, the exam is the most error-prone: is int x = 1, y = 0, z = 2;

x "y"z is true or false? Bringing in 1 "0 "2, it is definitely wrong from a mathematical point of view, but if it is C language then it is correct! Because 1 "0 is false to get 0, the expression becomes 0" 2 then the result of the operation is 1, which is called true!

c, the difference between the equal sign and the assignment! Be sure to remember that "=" is the assignment, and "==" is the equal sign. Although many people can recite it, I still want everyone to remember it. Otherwise, if I am wrong, I will definitely despise you!

2) Logical expression:

Core: The value of an expression can only be 1 (represented as true), or 0 (represented false).

a) Total && || ! Three logical operators.

b) ! 》&&”|| Priority level.

c) Pay attention to the short circuit. The exam is more like the exam. For details, please see the examples in the book. You must do examples 1 and 2

d) indicates a method in which x is less than 0 and greater than 10.

0 "x "10 is not acceptable (must remember). It is first calculated 0 "x results are 1 or 0; then 0, or 1 and 10 are always true (for 1). So be sure to use (0 "x) & & (x "10) means greater than 0 is smaller than 10.

3) if statement

a, else is the match with the nearest if and no else statement.

b, the program of the exchange: t = x; x = y; y = t;

c, if (a "b) t = a; a = b; b = t; if (a "b) {t = a; a = b; b = t;} the difference between the two, the test has been repeatedly tested !

d, a separate if statement: if (a "b) t = a; standard if statement: if (a "b) min = a; else min = b; nested if statement: if (a "b) if (b)c)printf("ok!"); select one if statement if(a= =t)printf("a"); else if(b= =t)printf("b"); else if (c==t)printf("c"); else pritnf("d");

Through the exercises, you should be familiar with the above if statements!

Classic exam questions: Combine the above four if statement questions to make a question, answer the wrong, please break it yourself! ready, go!

Int a=1, b=0; if(!a)b++; else if(a= =0) if(a)b+=2; else b+=3; What is the value of b?

If you don't understand the problem, you should never break it yourself, so that people who can't do it will live reasonably.

The correct thing is that b is 3.

Int a=1, b=0;

If(!a)b++; is false and does not execute

Else if(a= =0) is a fake execution

If(a)b+=2; A nested if statement belonging to else if is not executed.

Else b+=3; If the if-else-if statement does not have a correct one, execute the else statement!

4) Conditional expression:

Expression 1? Expression 2: Expression 3

a. Exams: After the fake.

b. Note that when the value of expression 1 is non-zero, the value of expression 2 is used as the whole operation result. When the value of expression 1 is 0, the value of expression 3 is used as the whole result.

c, int a=1, b=2, c=3, d=4, e=5;

k=a》b? c:d"e? d:e; What is the value of k? The answer is san

5) switch statement:

a) The process of execution must be understood! The detailed process of the class is said, please understand yourself!

b) Pay attention to the difference between break and no break. In the two examples in the book, there is no break. As long as there is a case match, the rest must be executed. If there is break, the swiche statement is directly jumped out. Break is a breakup in the C language, meaning a break.

c) switch can only be used with break, not with continue.

d) switch(x) x: is an integer constant, a character constant, and an enumerated data.

{case 1: .... It can't be a variable. Case 2: .... } e)switch is a compulsory question type, please be sure to complete the exercise of the switch after class.

University C language review focus

1) Program structure is three kinds: sequential structure, selection structure (branch structure), and loop structure.

2) The read program should be read from the main() entry, and then read from the top (down to the loop, and choose to make the selection). There is only one main function.

3) The computer's data is stored in the computer in binary form. The location where the data is stored is his address.

4) Bit is a bit that means 0 or 1. Byte is the byte, one byte = eight bits.

The concept is often tested:

1, the compilation pre-processing is not part of the C language, does not account for the running time, do not add a semicolon. A program compiled in C language is called a source program, and it is stored in a text file as an ASCII value.

2, define PI 3.1415926; This is wrong, must not appear semicolon.

3. There is one and only one main function in each C language program.

4. You cannot define a function in a function.

5. Algorithm: There can be no input, but there must be an output.

6, break can be used for loop structures and switch statements.

7. The comma operator has the lowest level and the assigned level is the second lowest.

Structural understanding of C language program based on C language basic knowledge

Using a simple c program example, introduce the basic structure, format, and good writing style of the C language, so that the small partners have a preliminary understanding of the c language.

Example 1: A c program that calculates the sum of two integers:

#include main() { int a,b,sum; /*Define the variable a,b,sum as an integer variable */ a=20; /* assign the integer 20 to the integer variable a*/ b=15; * Assign the integer 15 to the integer variable b*/ sum=a+b; /* assign the sum of the two numbers to the integer variable sum*/printf ("a=%d,b=%d,sum=% d", a, b, sum); /* output the calculation result to the display */ }

Key notes:

1. Any c language program must include the following format:

Main() { }

This is the basic structure of the C language, and any program must include this structure. You can not write anything in parentheses, then the program will not execute any results.

2, main () --- in the c language called "main function", a c program has only one main function, any c program always starts from the main function, a pair behind the main function Parentheses cannot be omitted.

3. The content enclosed by braces { } is called the function body of the main function. This part is the content to be executed by the computer.

4. There is a semicolon (;) after each sentence in { }. In c language, we call a sentence ending with a semicolon a c language statement, and the semicolon is the end of the statement.

5, printf ("a=%d, b=%d, sum=%d", a, b, sum); ---- by executing this c language system provides us with the screen output function directly used, the user You can see the results of the operation. After the program runs, the following results will be displayed on the display:

a=20, b=15, sum=35

6, #include

Note: (1) Start with ## (2) Do not end with a semicolon. This line has no semicolon, so it is not a statement. It is called a command line in C language, or it is called a "precompiled processing command".

7. The part of the program that starts with /* and ends with */ indicates the comment part of the program. Comments can be added anywhere in the program, added to improve the readability of the program, but the computer completely ignores the contents of the main function. The comment section, in other words, the computer as a comment part does not exist in the main function.

C program generation process

The C program is compiled from the source file to generate the target file, and then connected to generate the executable file.

The extension of the source program is .c , the extension of the target program is .obj , and the extension of the executable program is .exe.

Identifier

When writing a program, you must name functions, variables, and so on. This name is called an identifier. The naming rules for identifiers in C are as follows:

Identifiers can only consist of letters, numbers, and underscores;

The first letter of the identifier must be a letter and an underscore;

Identifiers distinguish between uppercase and lowercase letters, such as If and if are two completely different identifiers.

The legal identifiers are as follows: A6, b_3, _mn Illegal identifiers are as follows: ab#12 , 8m , tr3:4 , yes no

The identifier cannot be the same as the keyword with special meaning in the program. It cannot be the same as the function name and C language library function compiled by the user. In the program, the various identifiers should not be repeated as much as possible to distinguish. When choosing variable names and other identifiers, you should pay attention to "knowing the name".

The identifiers are divided into the following three categories:

1, the keyword

A keyword is a type of identifier that has a specific meaning and is specifically used to describe a specific component of the c language and cannot be used as an identifier for the user.

Auto break case char union do double else enum extern goto if int long short signed static sizof struct switch unsigned void for while typedef continue float return typedef default

2, predefined identifier

Predefined identifiers also have a specific meaning in the C language, but can be used as user identifiers. The predefined identifiers fall into two categories:

(1), library function name, such as (printf, scanf, sin, isdigit, etc.) (2), compile processing command name, such as (define, include)

3. User identifier

The identifier that the user defines himself or herself is called a user identifier. Regardless of how you customize the identifier, you must conform to the three naming conventions for the identifier.

constant

The amount by which a value cannot be changed during a program run is called a constant. There are five types of constants: integer constants, real constants, character constants, string constants, and symbolic constants.

(1) Numerical conversion

Four manifestations of numbers:

1: Binary: All numbers consist of 0,1, and every two digits will not appear in the binary number. Example: 2: Octal: Start with the number 0 (note that it is not the letter O, o), all numbers are composed of 0~7, every eight digits, one will not appear in the octal number. Example: 0112, 0123, 077, etc. 3: Decimal: All numbers are composed of 0~9. Every tenths and one decimal, 10 will not appear in the decimal number. Example: 0, 12, -15, etc. 4: Hexadecimal: Start with 0x or 0X (number 0 plus letter x), all numbers are composed of 0~9, A~F (or a~f), every sixteen Further (where A, B, C, D, E, F represent 10, 11, 12, 13, 14, 15 respectively) Example: 0x4A, 0X14c7, etc.

Inside the computer, the numbers are represented and stored in binary form. The ordinary decimal digits input by the user must be converted into binary by the computer to be stored in the computer. Similarly, the operation result of the computer is also binary. Generally, it is converted into a decimal number. The output is read to the user and this conversion is usually done automatically by the computer.

(1) Convert decimal to binary, octal, and hexadecimal

Divide: Divide the decimal number by 2, record the remainder, and the obtained quotient continues to be divided by 2 until the quotient is 0, and then the remainders obtained from each other are arranged in reverse order from the back to the front, and the resulting remainder number sequence is the corresponding decimal number. Binary number. The octal and hexadecimal conversion methods are the same as above.

Example: The value of the decimal number 13 converted to a binary number is 1101, the conversion octal is 015, and the conversion to hexadecimal is D.

(2) Convert binary, octal, and hexadecimal to decimal

Product summation: Multiply each bit of the binary from low to high (lower right, left high) by 20, 21, 22 respectively. . . . And then sum these products together.

For example: =(13)10 (317)8= (23E)16=

(3) Conversion between binary and octal, hexadecimal numbers

1: Binary to octal: Converts every three digits from right to left into a decimal number, and the resulting data combination is the corresponding octal number (note: the high digit is less than three digits). Example: (010 110 111) 2=(267)8 2: Binary to Hexadecimal: Convert every four digits from right to left into a decimal number, and combine the resulting data into the corresponding hexadecimal number (note : The high position is less than four digits to fill the zero). Example: (0101 1011) 2 = (5B) 16 3: Octal conversion binary: Each digit is converted to a three-digit binary number Example: (13) 8 = (001 011) 2 = (Note: remove the first two 00 Because 0 has no meaning in the high position. 4: Hexadecimal conversion binary: Each digit is converted to a four-digit binary number: (E3)16=(1110 0011)2

b) integer constant

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

(Note: There are no integer constants in the c language that directly represent binary. Binary does not appear in the c language source.)

Write as follows:

Decimal integer constants: 123, 0, -24, 85L (long integer constant) and other octal integer constants: 051, -026, 0773, etc. Hex integer constants: 0x55, 0x1101, 0x, 0x5AC0, -0xFF . Where L is a long integer.

(3) Real constants

Real constants come in two forms: decimal form and exponential form.

Decimal form: 5.4 0.074 -23.0 Exponential form: 5.4e0 4.3e-3 -3.3e4

(1) A real constant with a fractional part of 0, which can be written as 453.0 or 453. (2) When expressed in decimals, the two sides of the decimal point must have a number, which cannot be written as ".453" and "453.", but should be written as "0.453" and "453.0". (3) When using the index method, there must be a number before e, and the index after e must be an integer (note: the integer order code can be positive, negative, or octal, hexadecimal, but must be an integer) .

(four) character constants

The character constant is marked with a pair of single quotes ' ', and there are two types of character constants in the c language:

(1) A character enclosed by a pair of single quotes, such as 'a ', 'r', '#'. Note: 'a' and 'A' are two different character constants.

(2) enclosed by a pair of single quotes, beginning with a backslash \ followed by a number or letter, such as '', where "\" is the meaning of escaping, followed by different characters to indicate different meanings, such Character constants are called escape characters. The details are as shown.

The meaning of the escape character escape character ASCII code

Carriage return linefeed 10 Skip to the next tab position 9 \b Backspace 8 Enter 13 \f Paper feed 12 \\ Backslash "\" 92 \' Single quotation mark 39 \" Double quotation mark 34 \a Ringing 7 \ddd 1~3 digits octal number represented by character \xhh 1~2 digits represented by hexadecimal number

Five) string constants

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

Example: "ni hao" "happy" and so on.

(6) Symbolic constants

The symbolic constant is a constant defined by the macro definition "#define", and the identifier available in the C program represents a constant.

Example: The c program for calculating the area of ​​a circle.

#include #define PI 3. main() { float r,s; r=12.5; S=PI *r*r; printf("s= ​​%f ”,s); }

Description:

#define is a macro definition. All the places where PI appears in this program represent 3., and PI is called a symbol constant. It is customary to use uppercase letters to represent symbolic constants and lowercase letters to represent variables, which makes it easier to distinguish.

variable

A variable is the amount by which its value can change. A variable must have a variable name that occupies a certain memory location in memory, and the value stored in the memory location is the value of the variable. Different types of variables have different sizes of their storage units, and variables must be defined before they can be used.

(1) Integer variables

Integer variables are divided into four types: basic (int), short (short int or short), long int (long int or long), and unsigned int (unsigned short, unsigned long).

Different compiler systems have different specifications for the number of bits and values ​​occupied by the above four types of integer data.

Type specifier

University c language must back to the basic knowledge _c language basic knowledge Daquan

The word signed indicates "signed" (that is, there are positive and negative numbers). If you do not write signed, it is implicitly indicated as signed. Unsigned is used to describe "unsigned" (only positive numbers).

(2) Real variables

In C language, real variables are classified into single precision type ( float ) and double precision type ( double ). Such as:

Float a , b ; double m ;

In vc, float type data occupies 4 bytes (32 bits) in memory, and double type data occupies 8 bytes. Single-precision real numbers provide 7 significant digits, and double-precision real numbers provide 15 to 16 significant digits. Real constants are not divided into float and double. A real constant can be assigned to a float or double variable, but the variable intercepts the corresponding significant number in the real constant according to its type.

Note: Real variables can only store real values. You can't store real values ​​with integer variables, or you can use real variables to store integer values.

(three) character variables

Character variables are used to store character constants, defining the form:

Char variable name;

The keyword char defines a character data type, which occupies one byte of storage unit.

Example: char cr1, cr2; cr1= 'A', cr2='B';

When a character is assigned to a character variable, the character itself is not stored in memory, but the ASCII code corresponding to the character is stored in the memory unit. For example, the character 'A' has an ASCII code of 65 and is stored in memory as follows:

Since the characters in the memory are stored in ASCII code, its storage form is similar to the storage form of the integer, so the character data in the C language can be used in common with the integer data. One character can be output in the form of characters, and integers can also be used. The form output, character data can also perform arithmetic operations, which is equivalent to computing their ASCII code.

Automatic conversion and cast of types

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

Char " int " float " double

That is, the type of "low" on the left level is converted to the right. Specifically, if the highest priority data in the expression is double, then the other data in this expression is converted to double, and the result is also double; if the highest priority data in the expression Is a float type, then other data in this expression is converted to a float type, and the calculation result is also a float type.

In the assignment operation, if the type of the left and right sides of the assignment number is different, the type to the right of the assignment number is converted to the left type; 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 a cast, the representation is:

(type) (expression); Example: (int)(a+b)

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

C operator recognition

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

, arithmetic operators: used for various types of numerical operations. It includes seven kinds of addition (+), minus (-), multiplication (*), division (/), remainder (%), self-increment (++), and decrement (--).

, assignment operator: used for assignment operations, divided into simple assignment (=), compound arithmetic assignment (+=, -=, *=, /=, %=) and compound bit operation assignment (&=,|=,^ =, "" =, "" = three categories of a total of eleven. "=""" span=""

, comma operator: used to combine several expressions into an expression (,).

, relational operators: used for comparison operations. It includes six types: greater than ("), less than ("), equal to (==), greater than or equal to (= "" =), less than or equal to ("=" and not equal to (!=). "=""" span=""

, logical operators: used for logical operations. Including (&&), or (||), non (!) three.

Conditional operator: This is a trinocular operator for conditional evaluation (?:).

, bit operation operator: the amount of participation in the operation, the operation is performed in binary bits. Including bits and (&), bit or (|), bit not (~), bitwise or (^), left shift ("", right shift (")" six kinds.

8, pointer operator: used to take the content (*) and take the address (&) two kinds of operations.

9, the byte count operator: used to calculate the number of bytes (sizeof) of the data type.

10. Special operators: There are several types of brackets (), subscripts [], members (→, .).

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 +, -) and trinocular operators (such as ? : ).

Arithmetic operators and arithmetic expressions

First, the basic arithmetic operators

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

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

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

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

The operation of / is divided into two cases:

a, when the left and right sides of "division" are integers, the result must be an integer (note: only the integer part, not rounded)

For example, the value of 5/2 is 2, not 2.5, and the value of 1/2 is 0.

b. When at least one of the left and right sides of the "divide" is real data (ie, a decimal), the result is real data.

For example: the value of 5/2.0 is 2.5, and the value of 7.0/2.0 is 3.5.

(5)% (modulo operator or remainder operator, % should be integer data on both sides, such as 9%7 value of 2).

It should be noted that when the operand is negative, the result varies with the compiler. In vc, the resulting symbol is the same as the dividend, for example: 13%-2 is 1, and -15%2 is - 1.

Second, the priority and combination of arithmetic expressions and operators

Arithmetic expressions are expressions that conform to the C grammar rules by concatenating the operands (also called operands) with arithmetic operators and parentheses. The operands include functions, constants, variables, and so on.

In computer language, the law of evaluating arithmetic expressions is similar to the law of four arithmetic operations in mathematics. The rules and requirements of the arithmetic rules are as follows.

(1) In arithmetic expressions, multiple layers of parentheses can be used, but the parentheses must be paired. The operation starts from the inner parentheses and sequentially calculates the values ​​of the expressions from the inside to the outside.

(2) In the arithmetic expression, for operators of different priorities, the operator's priority can be calculated from high to low. If the operators in the expression have the same priority, the operator is combined in the direction of the operator. Operation.

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

Third, the self-increase and decrement operators

Role: Increase or decrease the value of the variable by 1.

Such as: ++i, --i (Before using i, first increase the value of i by 1, minus 1). i++,i-- (After using i, increase the value of i by 1, minus 1).

(1) Only variables can use the increment operator (++) and the self-decimation operator (--), while 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++, the left side of the i is the minus operator, the right side is the increment operator, the negative operation and the increment operation are "self" Right to left "combined, equivalent to - (i++).

In the loop statement, the auto increment (subtraction) operator is often used. This operator is also commonly used in pointers. Candidates should figure out "i++" and "++i" and "i--" and "--i". The difference, especially the value of the expression and the value of the variable.

Assignment Operators and Assignment Expressions I, Assignment Operators and Assignment Expressions

The assignment symbol "=" is an assignment operator that assigns a data to a variable or assigns the value of one variable to another. An expression consisting of an assignment operator is called an assignment expression. The general form is:

Variable name = expression

In a program, a variable can be assigned multiple times. Each time a value is assigned, the data in the corresponding storage unit is updated once. The current data in the memory is the data that was last valued.

Example: a=12; This expression is read as "Assign the value of 10 to the variable a".

a. If the operation object types on both sides of the assignment number are inconsistent, the system will automatically perform type conversion. The conversion rule: converts the type of the value of the expression on the right side of the assignment number to the type of the variable on the left side of the assignment number.

Example: int y=3.5; The final stored in the variable y is the integer 3.

b. The value of the copy expression can be assigned to the variable to form a continuous assignment.

For example: x=y=25 is a continuous assignment expression, x=y=25 is equivalent to x=(y=25), so the expression x=y=25 is the final value of 25.

Second, the compound assignment operator

Adding other operators before the assignment operator forms a compound assignment operator. The compound operators related to arithmetic operations are: +=, -=, *=, /=, %=.

There can be no spaces between the two symbols, and the composite assignment operator has the same precedence as the assignment operator. The expression n+=1 is equivalent to n=n+1. The effect is to increment the value of the variable n and assign it to the variable n. The arithmetic rules of other compound assignment operators are analogous.

For example, to express the value of a+=a-=a*a, where the initial value of a is 12 .

step:

(1) First perform the "a-=a*a" operation, which is equivalent to a=aa*a=12-144=-132. (2) Perform the "a+=-132" operation again, which is equivalent to a=a+(-132)==-264.

Comma operator and comma expression

In the C language, in addition to being a separator, a comma can also be used as an operator----comma operator, which joins several expressions with a comma operator, such as a=b+c, ​​a=b* c is called a comma expression.

The general form is:

Expression 1, expression 2, expression 3, ..., expression n

Example: x=2, y=3, z=4

The comma expression has a combination of left to right, that is, the expression 1 is solved first, and then the expression 2 is solved in order until the value of the expression n. The value of the expression n is the value of the entire comma expression. The value of the above comma expression is the value of the expression z=4. 4. Note that the comma operator is the lowest level of all operators.

Example: There are the following program segments:

Main() { int a=2,b=4,c=6,x,y; y=(x=a+b),(b+c); printf(“y=%d,x=%d” ,y,x); }

The program displays the result as: y=6, x=6

Discussion: What is the result of changing y=(x=a+b), (b+c); to y=((x=a+b),b+c)?

Relational operators and relational expressions 1. Logical values ​​in C language

There are only two logical values ​​in C: true (true) and false (flase). Use non-zero to represent truth and zero to represent false. Therefore, for any expression, if its value is zero, it represents a false value, and if its value is non-zero, it represents a 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 of -5 is true.

Second, the logical expression

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

(1) a&&b: When both && are "true", the value of the expression a&&b is true.

It is worth noting that in mathematics, relational

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

(3)! a: indicates negation, if a is true, then! A is false and vice versa. E.g! The value of -5 is 0.

In C language, a logical expression consisting of && or || produces a "short circuit" under certain circumstances.

(1) x && y && z , only when x is true (non-zero), it is necessary to determine the value of y; only when x and y are true, it is necessary to determine the value of z; as long as x is false, it is not necessary Determining y and z, the value of the entire expression is 0. Mouth: "A fake must be fake."

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

(2) x||y||z, as long as the value of x is true (non-zero), it is not necessary to discriminate the values ​​of y and z. The value of the whole expression is 1, and only the value of x is false. The value of x, only the value of x and y at the same time need to determine the value of z, the mouth: "one must be true."

Bitwise operation , bitwise operator

In a computer, data is stored in binary numbers, and bit operations are operations on binary bits in a memory location. The C language provides six bit operators.

Second, bit operation

The bitwise operator & |~ """ ∧ The order of priority from highest to lowest is:

The negation operation in the bit operator "~" has the highest priority, while the left and right shifts are the same, ranking second, and the next order is bitwise with "&", bitwise XOR "∧" and bitwise or " |". The order is ~ """ & ∧ |

Example 1: The left shift operator "" is a binocular operator. Its function shifts all the binary digits of the operand on the left side of "" to the left by several digits. The number on the right side of the """ specifies the number of digits to be moved, the high digit is discarded, and the low digit is zero. ="" """" span=""

For example: a "" 4 refers to shifting each binary of a to the left by 4 digits. For example, a=00000011 (decimal 3), after shifting 4 bits to the left, it is 00 (decimal 48).

Example 2: The right shift operator """ is a binocular operator. Its function is to shift all the binary digits of the operand on the left side of """ to the right by several digits. The number on the right side of """ specifies the number of digits to be moved.

For example: Let a=15, a》》2 means shift right to decimal 3).

It should be noted that for signed numbers, the sign bit will move with the right shift. When it is positive, the highest bit is 0, and when it is negative, the sign bit is 1, and the highest bit is 0 or 1 depending on the compiler system.

Example 3: Let the binary number a be 00. If the higher 4 bits of a are inverted by the exclusive OR operation a∧b, and the lower 4 bits are unchanged, the binary number b is.

Analysis: XOR operation is often used to flip a specific bit, as long as the bit to be flipped is XORed with 1 because the value of the original value of 1 is XORed with 1 to get 0, the median of the original The bit that is 0 is XORed with 1 and the result is 1. The bit that is XORed with 0 will retain the original value. XOR operations can also be used to exchange two values ​​without a temporary variable.

For example, int a=3, b=4;, if you want to exchange the values ​​of a and b, you can use the following statement: a=a∧b;

b=b∧a;

a=a∧b;

So the answer to this question is: .

As a language that is very suitable for programming, C language is self-evident.

ZGAR AZ Vape Pods 5.0S

ZGAR AZ Vape Pods 5.0S

ZGAR electronic cigarette uses high-tech R&D, food grade disposable pod device and high-quality raw material. All package designs are Original IP. Our designer team is from Hong Kong. We have very high requirements for product quality, flavors taste and packaging design. The E-liquid is imported, materials are food grade, and assembly plant is medical-grade dust-free workshops.

From production to packaging, the whole system of tracking, efficient and orderly process, achieving daily efficient output. WEIKA pays attention to the details of each process control. The first class dust-free production workshop has passed the GMP food and drug production standard certification, ensuring quality and safety. We choose the products with a traceability system, which can not only effectively track and trace all kinds of data, but also ensure good product quality.



We offer best price, high quality Pods, Pods Touch Screen, Empty Pod System, Pod Vape, Disposable Pod device, E-cigar, Vape Pods to all over the world.

Much Better Vaping Experience!




ZGAR AZ Pods 5.0S Pods,ZGAR AZ Vape Pods 5.0S,ZGAR AZ Vape Pods 5.0S Pod System Vape,ZGAR AZ Vape Pods 5.0S Disposable Pod Vape Systems

Zgar International (M) SDN BHD , https://www.sze-cigarette.com

Posted on