6C Language Operator

Operators and Expressions

Operator

Classification:

  • Arithmetic operator
  • Shift operators
  • Bitwise operators
  • Assignment operator
  • unary operator
  • Relational Operators
  • Logical Operator
  • Conditional Operator
  • comma expression
  • The structure members of the following table references, function calls

Arithmetic operator

+	-	*	/	%
  1. All but%can be used for operations between integers and floating-point numbers
  2. Division operator'/': Performs an integer division if both operands are integers. As long as there are floating-point numbers in it to perform the operation, the whole expression is a division of floating-point numbers.
  3. The two operands of%must be integers.

Shift operators

<< Left shift 			>> Right Shift	

Left Shift Left Discard Right Complement 0

Right Shift

  1. Arithmetic Right Shift

Right discard, left complement bit (*)

  1. Logical Right Shift

Discard on right, add 0 on left

Warning: For shift operators, do not move negative digits. This standard is undefined.

int num = 10;
num>>-1;//FALSE

Bitwise operators

Bit operators:

&		Bitwise and			 0 is zero
|		Bitwise or			 One is one
^		xor		Same as 0 but different as 1

The operand must be an integer.

Small exercises

int num1 = 1;
int num2 = 2;
printf("%d  ",num1&num2);
printf("%d  ",num1|num2);
printf("%d  ",num1^num2);

Question: Exchange of two numbers without creating a temporary variable

Way of thinking (addition and subtraction may overflow)

code implementation

int a = 2;
int b = 3;
a = a+b;
b = a-b;
a = a-b;

Method two (thinking) a pair of numbers is the same number twice exclusive or back to itself. And Exclusive Or operations have the law of exchange

code implementation

int a = 2;
int b = 3;
a = a^b;
b = a^b;
a = a^b;
printf("%d  ",a);
printf("%d  ",b);

Practice:

The number of 1 integers stored in the memory binary.

Ideas: 1. The idea of short division. (Only positive numbers can be calculated).

  1. Calculating bitwise with 1 gives you what the last bit is, plus a right shift.

Code implementation:

int main(){
    int number = 0;
    int count = 0;
    scanf("%d",&number);
    //Negative numbers cannot be calculated
    // while(number){
    //     if (number%2 == 1){
    //         count++;
    //     }
    //     number = number /2;
    // }
    for(i = 0;i<32;i++){		//32 because int is 32
        if(1==((num>>i)&1))
            count++;
    }
    printf("%d",count);
    return 0;
}

Assignment Operator (=)

Initialization assignment, reassignment, continuous assignment.

Composite assignment operator
+=    -=    *=    /=    %=    >>=    <<=    &=    |=    ^=	

unary operator

OperatorEffect
!Logical negation
-negative
+just
&Take Address
sizeofType length of operand (in byte bit units)
~Bitwise Reverse
Front and rear autosubtraction
++Front and rear self-additions
*Indirect access operator (dereference operator)
(type)Forced Type Conversion

Positive and negative are monotonic operators, and addition and subtraction are binary operators

1 is true but not necessarily 1.

Calculate Array Length= sizeof(arr)/sizeof(arr[0])

Puzzle:

int main(){
    short s = 0;
    int a = 10;
    printf("%d\n",sizeof(s = a + 5));
    printf("%d\n",s);
}

Resolution: The size of sizeof(s = a+5) ultimately depends on the type of S.

s==5 is because the expression in the sizeof operator is just an expression and does not really participate in the operation.

sizeof and array
void test1(int arr[]){
    printf("%d  ",sizeof(arr));//(3)
}
void test2(char ch[]){
    printf("%d\n",sizeof(ch));//(4)
}
int main(){
    int arr[10] = {0};
    char ch[10] = {0};
    printf("%d  ",sizeof(arr));//(1)
    printf("%d\n",sizeof(ch));//(2)
    test1(arr);
    test2(ch);
    return 0;
}

Resolution:

(1) = sizeof(int)*10 = 40

(2) = sizeof(char)*10 = 10

(3) = sizeof(arr first element pointer) = 8 or 4 (64-bit computer and 32-bit computer)

(4) = sizeof (pointer to the first element of ch) = 8 or 4 (64-bit computer and 32-bit computer)

Relational Operators

Relational Operators
>    >=    <    <=    !=    ==
Logical Operator
&&    ||

Distinguishing Logical And Bitwise And&And&

Distinguish Logical Or from Bitwise Or || and |

Logic is two, bitwise is one.

Written Test

What happens when the program runs?

int main(){
    int i = 0,a=0,b=2,c=3,d=4;
    i = a++ && ++b &&d++;
    //i = a++||++b||d++;
    printf("a=%d  b=%d  c=%d  d=%d",a,b,c,d);
    return 0;
}

The result is a=1 b=2 c=3 d=4

Parse: Logic and left false right side are not calculated. In a++, first use then operation. The first part of formula i is false.

What happens when the program runs?

int main(){
    int i = 0,a=0,b=2,c=3,d=4;
    i = a++||++b||d++;
    printf("a=%d  b=%d  c=%d  d=%d",a,b,c,d);
    return 0;
}

Run result: a=1 b=3 c=3 d=4

Parse: Logical or left true. On the right there is no calculation. a++ runs false, ++ b runs true. d++ will not run.

Conditional Operator (Trinomial Operator)
Expression 1?Expression 2:Expression 3

comma expression

Expression 1,Expression 2,Expression 3,Expression 4...,Expression n

Calculate from left to right. The result of the entire expression is the result of the last expression.

int a = 1;
int b = 2;
int c = (a>b,a = b+10,a,b=a+1);
printf("%d",c);//The result is 13

The following table references, function calls, and structure members

1. [] Subscript reference operator

Operand: an array name + an index value

int arr[10];//Create Array
arr[9] = 10;//Use subscript reference operator
[]The two operands are arr And 9.

2. () A function calls an operator and accepts one or more operands: the first operand is the function name, and the remaining operands are the parameters passed to the function.

3. Access members of a structure

Structural objects. member

->Structure Pointer->Member Name

//Creating a struct type called member in Stu C is similar to an object-oriented property
struct Stu{
    char name[10];
    int age;
    char id[20];
};

int main(){
    //A Student object is created using the struct Stu type of structure and initialized
    struct Stu s1 = {"Zhang San",20,"201808520"};
    printf("%s\n",s1.name);
    printf("%d\n",s1.age);
    printf("%s\n",s1.id);
    struct Stu* ps = &s1;
    printf("%s\n",ps->name);
    printf("%d\n",ps->age);
    printf("%s\n",ps->id);
    return 0;
}

Expression evaluation

Part of the order in which expressions are evaluated is determined by the precedence and summation of operators.

Similarly, the operands of some expressions may need to be converted to other types during evaluation.

Implicit Type Conversion

C integer arithmetic operations are always performed with at least the precision of the default integer type.

To achieve this accuracy, the characters and short integer operands in the expression are converted to normal integers before they are used, a conversion called integer promotion.

The significance of integer promotion:

	The integer operation of the expression is to be performed in CPU Executed within the corresponding operating device, CPU Inner Integer Operator(ALU)The byte length of an operand is usually int Its own length, but also CPU Length of the universal register.
	So, just in time for two char Type addition, in CPU Actually, you also need to convert to CPU The standard length of a memory integer operand.
	currency CPU It is difficult to directly add two 8-bit bytes(Although there may be instructions for this byte addition in machine instructions). So the lengths in the expression may be less than int Length integer value, must be converted to int or unsigned int,Then you can enter CPU To perform an operation.

Example:

Q: What is the result of the output?

char a = 3;
char b = 127;
char c = a + b;
printf("%d\n",c);
//Output-126

Resolution:

char a = 3;
//00000000000000000000000000000011
//Truncate -> 00000011 -> Save to a
char b = 127;
//00000000000000000000000001111111
//Truncate -> 01111111 -> Save to b
char c= a+b;
//a:00000011 integer promotion - > (by symbol bit promotion)
//00000000000000000000000000000011
//b:01111111 Integer Lift->  
//00000000000000000000000001111111
//Addition
//00000000000000000000000010000010
//Truncate -> 10000010 -> Save to c
printf("%d\n",c);
//a:10000010 integer promotion - > (by symbol bit promotion)
//11111111111111111111111111110000010 - Complement
//11111111111111111111111111110000001 - Inverse Code
//10000000000000000000000001110 - Original
//Convert to decimal: -126

The values of b and c are promoted to normal integers and the addition is performed. After the addition operation is completed, the result is staged and stored in a.

So what is integer promotion?

Integer promotion is based on the symbol bits of the data type of the variable. The unsigned number can be directly preceded by 0.

# Integer promotion of negative numbers
char a = -1;
Integer Lift 11111111  (Complement Code)  Complement Code-1-> Inverse Code Positive Number Invariant Negative Number Reverse
# Integer promotion of positive numbers
char = 1;
Integer lift 00000001(Complement Code)   Positive numbers have the same three codes

Example of integer promotion

//Case 1
int main(){
    char a= 0xb6;//10110110
    short b = 0xb600;
    int c = 0xb6000000;
    if(a==0xb6)
        printf("a");
    if(b==0xb600)
        printf("b");
    if(c==0xb6000000)
        printf("c");
    return 0;
}

Explains the existence of integer promotion.

Arithmetic Conversion

If each operand of an operator is of a different type, the operation cannot proceed unless one of the operands is converted to the type of the other. Normal arithmetic conversion.

long double
double
float
unsigned long int
long int 
unsigned int 
int

If the type of an operand ranks lower in the list above, the operation is performed after converting to the type of another operand.

Note: Arithmetic conversion should be done properly or there will be some potential problems.

float f = 3.14;
int num = f;//Factor conversion, loss of precision.
Operator Properties

The evaluation of complex expressions has three factors.

  1. operator precedence
  2. Nuclearity of operators
  3. Whether to control the evaluation order

Which of the two adjacent operators does it hold first? Depends on priority. If the priority is the same, it depends on their combination.

operator precedence

Note: Pictures are quoted from [ https://blog.csdn.net/zhanghong056/article/details/76667298 ]

Problem code:

int main(){
    int i = 1;
    int ret = (++i) + (++i) +(++i);
    printf("%d",ret);
    printf("%d",i);
    return 0;
}
//It is preliminary to decide whether the order of calculation is the addition of three numbers, but whether the first ++ i is stored before calculation or if the value is changed before calculation. This is ambiguous.

Focus:

Writing an expression is problematic if the only calculation path cannot be determined by the properties of the operator.

Keywords: C Back-end

Added by flight553 on Wed, 05 Jan 2022 01:42:02 +0200