C Language-operator

Source: Internet
Author: User

One, Operator:

A symbol of a connection operand, used primarily for arithmetic calculations or logical judgments.

Classification:

1) Number of operands participating in the operation: Monocular operation (sizeOf () i++ Binocular Operation Trinocular operation (conditional operator)

2) Functionally: arithmetic operator relation and logical operator bit operator

Priority level:

The calculation sequence of different operators

Binding nature:

Prerequisite: Same Priority level

In the case of the same priority, which is calculated first

What are the arithmetic operators?

+-(minus) */%

An arithmetic operator is a binocular operation

Precedence: The order in which different operators are executed

Adhesion: Left-associative a+3-2+b//+-+---> Priority 4

Precautions

% redundancy

m%n

1) Note:

m = 0 The result is 0

n = 0 No meaning

M > N Normal take surplus

M < n result is M

2) The positive and negative nature of redundancy

The positive and negative nature of redundancy depends on the positive and negative of M

3) remainder can only be used for integer int i = 10.5%2 error

Third, data type conversion issues

Data type conversions are divided into two types: cast and implicit conversions.

Cast:

char c = ' A ';  int B = a + (int) C; Cast character C to int

Implicit conversions:

int a = 10; float B = 0.5f; Double F = a + B; B of type A and float type int is automatically converted to double type

Four: Assignment operators

Simple assignment operation: int a = 10+1; A = b = 10; Note that from right to left, because the assignment operator has the lowest precedence,

Note: You cannot assign a value to a constant of ten = A+b (Error)

So if the conditions in the IF function we want to prevent less write a "=" number (the compiler will not error) we generally write the constant on the left

Compound assignment Operation:

+ = a+=3//a = a+3;

-= a-=3//a = a-3;

*=

/=

%=

The calculation of the same assignment operator first considers the precedence of the operators, and then considers the binding order in the case of precedence

Five: self-increment, self-reduction

1> i++, which indicates that the value of I is first taken as the value of the expression A + i++-->a + 5, a = 6; ++i, which indicates that the value of the variable is first +1, and then I is taken as the expression of the values of the calculation

2>i++ first change, ++i first change after use

3> int a = 10;

There are four ways of adding a plus 1

(1) A = A+1, (2) a +=1; (3) a++; (4) ++a;

Note: int d=1++;//is wrong because it makes no sense to self-add and subtract from constants.

int i = 5, j = 5, p, q;

5/6 + 6/7 + 7/8

P= (i++) + (i++) + (i++);

q=j+++j+++j++;

printf ("%d,%d,%d,%d", p,q,i,j); 18 18 8 8

VI: sizeof operator

Function: Calculates the number of bytes allocated in memory for constants, variables, data types

Note: sizeof (char)//1 bytes; indicates that the program wants memory allocation char data type 1 bytes of memory space

sizeof (' a ')//4 bytes, character constant stored in memory with his ASCII code of-->int

sizeof ("AA")///3 bytes; The string in C is composed of a character array consisting of one character and 1 bytes, and the end of the string is terminated by the (= = String end)

1) When sizeof acts on constants and variables, parentheses can be omitted

2) sizeof cannot calculate the number of bytes consumed by a function

Seven, comma operator

Comma operator: ","

Comma expression: Use "," to concatenate multiple expressions together to form a new expression, a new expression called a comma-expression

D= (a+3,b*4)//comma-expression

D= ((a+3,b*4), a+b)//nested

The process of evaluating the value:

The value of each expression is evaluated (executed from left to right), and the value of the last subexpression is returned as the value of the entire expression.

Special: for (int i=0,b=3;i<4;i++,j--)

#pragma mark Sort

void Sort_list (Pnode phead)

{

int I, J;

Pnode p, q;

for (i = 0, p = phead->pnext; i<length_list (phead)-1; i++, p = p->pnext) {

for (j = i+1, q = p->pnext; j<length_list (phead); j + +, q = q->pnext) {

if (p->date>=q->date) {

int temp;

temp = p->date;

P->date = q->date;

Q->date = temp;

}

Eight, relational operators

a) Condition judgment

By default. The correct code for each sentence written in the program is executed, but many times we need to execute a certain piece of code, such as a login operation, in a condition that can be done using conditional statements.

(b) True and False

In C language, the condition is called "true", the condition is not established is called "false".

C language provisions, any value is true and false, any non-0 value is true, only 0 is False, no Boolean type.

(iii) Comparison of relationships

The result of the relational operation is only two cases, if the condition is true, the value is 1, if the condition is false, the value is 0. There are 6 types of operators, namely: (1) < (2) <= (3) > (4) >= (5) = = (6)! =

Nine, logical operators

The result of a logical operation is only two: true (0) and False (1)

①. and && conditions 1&& conditions 2 only that condition 1 and condition 2 are established, only for true otherwise false.

②. or | | Condition 1 | | Condition 2 When one of the conditions 1 or Condition 2 is true, it is false when it is not established.

③. Not!! Conditional inversion

Note: When a logical operator is operating, the following conditions are ignored, as long as the integrity of the whole can be determined.

Int a=b=10;

Int c= (a>5) && (++b>=11);//At this time a==10,b==11,c==1

Int c= (a<5) && (++b>=11);//At this time a==10,b==10,c==0

Tip: You can use () when you are doing a logical operation, if you don't know the priority of each symbol.

Note: Logical short-circuit problem:

    

Logic and short-circuit problems

B is already 0, the result must be 0 so, the back (c=b+5,c++) does not perform the

result = B && (c=b+5,c++); 0 0

Logic or short-circuit problems

2 A is 2 true (satisfies a true truth), | | The back is not executed.

result = A | | (c=b+5,c++);

Ten, conditional expressions

Format: expression 1? Expression 2: Expression 3;

The values of A and B are required to be compared to a large number in C c=a>b?a:b

Compares the value of the A,b,c three number and deposits the maximum value in D

Int abmax= (a>b)? a:b;

d=abmax>c:abmax:c;

C Language-operator

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.