Li Hongqiang-C language 7-c language operator

Source: Internet
Author: User
Tags logical operators

C-language operators

First, arithmetic operations

There are 34 types of operators in C, including common subtraction operations.

①. Addition: + can also indicate a plus sign

②. Subtraction:-can also represent a minus sign

③. Multiplication: * Non-mathematical meaning X

④. Division:/Note that the value of 1/2 is 0, not 0.5

⑤. Residual (modulo operation): the remainder after dividing two integers (note that both sides must be integers, with symbols only related to Lvalue)

Note the point:

①. Int a=10.8;//Data Precision loss warning. Result for 10--automatic type conversion

②. int a= (int) 10.8;//no warning. Cast 10.8 to integral type--coercion type conversion

③. Double c=10.6+6//The result is 16.600000. In a computer, with two numeric values, the values on both sides of the operator need to be of the same type and are automatically type promoted if different. That is, convert 6 to 6.000000 and then participate in the operation.

④. What type of two number is involved in the operation, then what is the result? An int A=10/3 result of 3,double B=10/3 results in a 3.000000;double C=10.0/3 result of 3.333333. An exact value is required to enforce type conversions. Double d= (double) 3/2 converts 3 to a double type to 3.000000, then 2 for automatic type promotion to 2.000000 participation, and 1.500000 for the result. Can also be written as 3/(double) 2. This situation requires attention to the difference from casting to the whole double e= (double) (3/2) is the first to calculate 3/2 for the value of 1 and then to force the type conversion to 1, the result is 1.000000.

Second, assignment operation

(i) Simple assignment

Int a=10+5;a=b=10;//from right to left, cannot be constant on left

(b) Compound assignment

a=a+5;=>a+=5;

a=a*5;=>a*=5;

a+=5*6+4;=>a=a+ (5*6+4)

Tip: The budget order depends on two points: the first is the precedence of the operators, and the second is the order of the combinations.

Three, self-increment self-reduction operation

Int a=10;

There are four ways to add the value of a to 1:

①. a=a+1;

②. a+=1;

③. a++;

④. ++a;

Simple distinction between a++ and ++a.

Int b;

Int a=10;

B=++a; A==11,b==11;a first add 1 to 11, then assign to B

b=a++; A==11,b==10;a first copy the value 10 to b,a and add 1 to 11.

b= (a++) + (++a); A1==10,a2==12,b==22

b= (++a) + (a++); A1==11,a2==11,b==22

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

Iv. Sizeof

Function: The number of bytes of memory used to calculate a variable, constant, or a data type.

Basic form: sizeof (variable name | constant | data type), returns a value after completion.

①. Sizeof Variable | constant

②. Sizeof (Variable | constant)

③. Sizeof (data type)

Note: Data types must be enclosed in parentheses and cannot be written in the form of the sizeof data type.

V. Relational operators

(i) 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)! =

(iv) Use of attention

①. = = and! = have equal precedence, and the other four relational operators have equal precedence, and the precedence of the former is lower than the latter. If 2==3>1 should calculate 3>1 first

②. In relational operators, if the precedence is the same, then "left to right" is combined. If 4>3<2, calculate 4>3 first

③. The precedence in the relational operator is less than the arithmetic operator. such as 3+4>8-2 equivalent (3+4) > (8-2)

④. Exercise 5! =4+2*7>3==10 Calculate 5 First! =18>3==10,5! =1==10,1==10, Fake

VI. 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.

Seven or three mesh operator

Binocular: Requires two values to participate in the operation

Monocular: A numeric participation operation such as! 5

Trinocular: Requires 3 values to participate

Format: Condition? Value 1: Value 2

Judge first? Before the conditions, if the condition is set to return a value of 1, if the condition is not established, return to the condition 2.

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;

Li Hongqiang-C language 7-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.