An operator is a symbol that operates on a value or a variable.

  • C 语言与其他高级语言相比, 运算符特别丰富, 共有 34 种运算符.
  • C 语言将这 34 种运算符规定了不同的 优先级 & 结合性.
    • 优先级, 是用来标识运算符在表达式中的运算顺序的, 在求解表达式的值的时候, 总是先按运算符的优先次序由高到低进行操作;
    • 结合性, 当一个运算对象两侧的运算符 优先级相同 时, 则按运算符的结合性来确定表达式的运算顺序.
  • 优先级越小, 优先级越高:
    Referecne Material: https://blog.csdn.net/hitwhylz/article/details/14526569
    • 优先级(高--> 低): 单目运算符 > 双目运算符 > 三目运算符
    • 一级运算符: 括号运算符((),[]), 成员运算符(.,->)
    • 二级运算符: 单目运算符, 强制类型转换, sizeof
    • 算数运算符:(双目运算符 3 ~ 12 级)
      • 3 级运算符: /, *, %
      • 4 级运算符: +, -
    • 5 级运算符: 移位运算符(<<,>>)
    • 比较运算符:
      • 6 不等比较: >, >=, <, <=
      • 7 相等比较: ==, !=
    • 8 ~ 12 级运算符: &, ^, |, &&, ||
    • 13 级运算符: 条件运算符(三目运算符 13级)
    • 14 级运算符: 赋值运算符
    • 15 级运算符: 逗号运算符
  • 结合性:
    如果在一个运算对象两侧的运算符具有相同的优先级, 则按规定的"结合方向"处理.
    • 左结合性(从左到右): 1 , 3 ~ 12, 15 级运算符
    • 右结合性(从右到左): 二级运算符, 13 级三目运算符, 14 级赋值运算符

自增/自减运算符(右结合性)

C programming has two operators increment(++) and decrement(--) to change the value of an operand (constant or variable) by 1.

  • These two operators can be used as prefixes like ++a and --a.
  • These two operators can also be used as postfixes like a++ and a--.
  • ++, -- 前置 & 后置, 只用与变量, 不能用于常量和表达式.(因为隐含有赋值操作)

算数运算符 Arithmetic Operators

An arithmetic operator performs mathematical operations such as addition(or unary plus)(+), subtraction(or unary minus)(-), multiplication(*), division(/), modulo division(remainder after division)(%) etc on numerical values (constants and variables).

  • The % operator can only be used with integers.
  • % , 两数相除后, 余数符号与被除数相同 (如: -5%2 == -1)
  • / , 参与运算有一个是浮点型, 则结果为 double

关系运算符 Relational Operators

A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0.

位运算 Bitwise Operators

During computation, mathematical operations like: addition, subtraction, multiplication, division, etc are converted to bit-level which makes processing faster and saves power.

  • Bitwise operators are used in C programming to perform bit-level operations.
  • ~(complement), <<(shift left), >>(shift right), &(AND), ^(exclusive OR), |(OR)
  • 位运算符只能用于整型操作数, 而不能用于浮点型数据.
    • << 左移, 右边补 0
    • >> 右移, 左边补 0(无符号数) or 最高位补符号位(有符号数). (因系统不同会有所差别)

逻辑运算符 Logical Operators

An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false.

  • !, &&, ||
  • 非 0 值 ---> true
  • 0 ---------> false

赋值运算符 Assignment Operators (右结合性)

An assignment operator is used for assigning a value to a variable.

  • 简单赋值 : =
  • 复合算数赋值 : +=, -=, *=, /=, %=
  • 复合位运算赋值: &=, |=, ^=, >>=, <<=
  • 赋值表达式的结果 == 最左边赋值运算符左边变量(或表达式)的值.

Other Operators

  • Comma Operator(,) :
    • Comma operators are used to link related expressions together.
    • 把两个表达式连接起来组成一个表达式, 称为逗号表达式.
    • 整个逗号表达式的值 == 逗号表达式中最后一个表达式的值.
  • The sizeof operator: The sizeof is a unary operator that returns the size of data (constants, variables, array, structure, etc). (return %lu bytes)
    • the sizeof operator returns size_t (unsigned integral type).
    • The size_t data type is used to represent the size of an object. The format specifier used for size_t is %zu.
  • 三元运算符 ternary operator: (condition) ? (result1) : (result2)
  • 指针运算符:
    • 取地址运算符/引用运算符 reference operator : &
    • 取值运算符/解引用运算符 dereference operator: *
  • 成员选择运算符 member selection operator: .(对象), ->(指针)
  • 特殊运算符:括号(), 下标[], 成员(->, .)
#include<stdio.h>

int main() {
    int a, b, c;
    // 赋值表达式的值, 就是赋值表达式左边变量所接收的值
    printf("%i\n", a=5);
    // 逗号表达式的值, 就是逗号表达式中最后一个表达式的值
    printf("%i\n", (a=1, b=2, c=3, a+b+c));
    // 非 0 值, 都为 true; false == 0
    if(-1)printf("-1 is true.\n");

    /* 从右往入栈, ++操作的执行顺序问题(下次研究)
    printf("%d \t %d \t %d \t %d\n", a, a++, ++a, a);
    a=-1;
    printf("%d \t %d \t %d\n", a++, ++a, a);
    a = 1;
    printf("%d \t %d \t %d\n", ++a, a++, a++);

    a=-1;
    printf("%d\n", a);
    printf("%d\n", ++a);
    printf("%d\n", a++);
    printf("%d\n", a);
    */

    return 0;
}





  • 在进行 表达式运算 & 赋值语句 时, 不同类型的数据先转换成同一类型, 然后进行计算或赋值.
    • 整型, 实型, 字符型 可以混合运算.
    • 类型转换都是临时转换, 不改变变量本身的类型
    • 自动转换/隐式转换:由编译系统自动完成;
    • 强制转换: 通过类型转换运算符来实现
  • 表达式 & 语句 的区别:
    • 表达式 = 操作对象 + 操作符
    • 表达式语句 = 表达式 + ';'