C language operators and expressions

1. Arithmetic operator

    +
    -
    *
    /     // 5 / 2 = 2
    %     // 5 % 2 = 1

Note:
1. In addition to the % operator, several other operators can work with integers and floating-point numbers, but it should be noted that the format and precision of the result printing are missing.
2. For the / operator, integer division is performed if both numbers are integers, and floating-point division is performed as long as there are floating-point numbers.
3. The two operands of the % operator must be integers. Returns the remainder after division.

2. Shift operator

Left shift operator <<

Shifting rules: discard on the left, zero-fill on the right

    int num = 10;    //00000000000000000000000000001010  num在内存中的二进制
    num = num >> 1;  //00000000000000000000000000010100  num左移一位的结果

right shift operator >>

1. Arithmetic shift (commonly used)

Shifting rule: fill the left side with the sign bit of the original value, discard the right side

2. Logical shift

Shifting rules: supplement with 0 on the left, discard on the right

    int num = -1;  //11111111111111111111111111111111  内存中-1的补码
    //算数右移  11111111111111111111111111111111  左边用原该值的符号位填充
    //逻辑右移  01111111111111111111111111111111  左边用0补充

3. Bit operators

    &    //按位与    两数补码同位比较,同为1为,其余为0
    |    //按位或    两数补码同位比较,有1就取1,都为0才取0
    ^    //按位异或  两数补码同位比较,相同取0,不同取1

Note: Operands must be integers

    #include <stdio.h>
    int main()
    {
        int num1 = 1;   //00000000000000000000000000000001
        int num2 = 2;   //00000000000000000000000000000010
        num1&num2;      //00000000000000000000000000000000      
        num1|num2;      //00000000000000000000000000000011
        num1^num2;      //00000000000000000000000000000011
    }

Example: Find the number of 1's in the binary of an integer stored in memory

    #include <stdio.h>
    int main()
    {
        int num = -1;
        int i = 0;
        int count = 0;    //计数
        for (i = 0; i < 32; i++)
        {
            if (((num >> i) & 1) == 1)
            count++;
        }
        printf("二进制中1的个数 = %d\n", count);
        return0;
    }

4. Assignment

compound assignment operator

    +=
    -=
    *=
    /=
    %=
    >>=
    <<=
    &=
    |=  

a + = 1, immediately a = a + 1

5. Unary operator

//逻辑反操作    对非0的数反为0,对0取反为1
    -         //负值
    +         //正值
    &         //取地址
    sizeof    //操作数的类型长度(以字节为单位)
    ~         //对一个数的二进制按位取反
    --        //前置、后置--
    ++        //前置、后置++
    *         //间接访问操作符(解引用操作符)
    (类型)    //强制类型转换

sizeof and arrays

    #include <stdio.h>
    void test1(int arr[])
    {
        printf("%d\n", sizeof(arr));    //4
    }
    void test2(char ch[])
    {
        printf("%d\n", sizeof(ch));    //4
    }
    int main()
    {
        int arr[10] = {0};
        char ch[10] = {0};
        printf("%d\n", sizeof(arr));    //40
        printf("%d\n", sizeof(ch));     //10
        test1(arr);
        test2(ch);
        return 0;
    }

++ and - - operators

Prepend ++, - -
    int a = 10;
    int x = ++a;    //x = 11    先对a进行自增,然后使用a
    int y = --a;    //y = 10    先对a进行自减,然后使用a
Postfix ++, - -
    int a = 10;
    int x = a++;    //x = 10  a = 11  先使用a,然后对a进行自增
    int y = a--;    //y = 11  a = 10  先使用a,然后对a进行自减
dereference operator
    int a = 10;
    int p = &a;    //定义指针p
    p = 20;        //解引用操作

6. Relational Operators

    >
    >=
    <
    <=
    !=    //用于测试“不相等”
    ==    //用于测试“相等”

Note: In the process of programming, == and = are written incorrectly, resulting in errors

7. Logical Operators

    &&    //逻辑与
    ||    //逻辑或

Note: In logical AND, once 0 appears, it will be 0, and the following will not be calculated.

Logical AND and Bitwise AND, Logical OR and Bitwise OR
    1&2 ------> 0
    1&&2 -----> 1

    1|2 ------>3
    1||2 ----->1

8. Conditional operator

    exp1 ? exp2 : exp3

    a > 5 ? b = 3 : b = -3

9. Comma Expression

exp1, exp2, exp3, ...expn

Comma expressions, executed in order 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 = a+10, a, b = a + 1);    //逗号表达式

10. Subscript references, function calls, and structure members

1.[ ]

Operands: an array name + an index value

    int arr[10];    //创建数组
    arr[9] = 10;    //使用下标引用操作符
    //[]的两个操作数是arr和9

2.( )

The function call operator
accepts one or more operands: the first operand is the function name, and the remaining operands are the parameters passed to the function

    #include <stdio.h>
    void test1()
    {
        printf("哈哈\n");
    }
    void test2(const char *str)
    {
        printf("%s\n", str);
    }
    int main()
    {
        test1();            //使用()作为函数调用操作符
        test2("hello");     //使用()作为函数调用操作符
        return 0;
    }

3. Access a struct member

.struct.member name
-> structure pointer -> member name

    struct Stu
    {
        char name[10];
        int age;
    }
    int main()
    {
        struct Stu stu;
        struct Stu* pStu = &stu;    //结构体成员访问
        stu.age = 20;               //结构体成员访问
        pStu->age = 20;             //结构体成员访问
        return 0;
    }

11. Expression evaluation

implicit type conversion

Integer arithmetic in c is always performed with at least the precision of the default integer type. To achieve this precision, character and short operands in expressions are converted to ordinary types before use, a conversion known as "integral promotion".

Arithmetic conversion

寻常算术转换
long double
double
float
uunsiged long int
long int
unsigned int
int

If the type of an operand ranks lower in the above list, the operation is performed first after conversion to the type of the other operand.

    float f = 3.14;
    int num = f;    //隐式转换,会有精度丢失

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325440441&siteId=291194637