运算符是任何编程语言的基础。因此,在不使用运算符的情况下,C语言的功能是不完整的。运算符允许我们对操作数执行不同类型的操作。在C中,运营商可以分为以下几类:

  • 算术运算符 s(+, - ,*,/,%,后增量,预增量,后减量,预减量)
  • 关系运算符(==,!=,>,<,> =&<=)逻辑运算符(&&,||和!)
  • 按位运算符(&,|,^,〜,>>和<<)
  • 赋值运算符 s(=,+ =, - =,* =等)
  • 其他运算符(条件,逗号,sizeof,地址,redirecton)

算术运算符:这些运算符用于对操作数执行算术/数学运算。属于这一类的二元运算符是:

    • 另外:“+”操作符添加两个操作数。例如,x + y
    • 减法:“ - ”操作减去两个操作数。例如,xy
    • 乘法:“*”操作符乘以两个操作数。例如,x * y
    • 部:“/”操作符除以所述第二第一操作数。例如,x / y
    • 模量:“%”操作符返回时第一操作数被第二分割余数。例如,x%y
// C program to demonstrate working of binary arithmetic operators
#include<stdio.h>
int main()
{
    int a = 10, b = 4, res;
    //printing a and b
    printf("a is %d and b is %d\n", a, b);
    res = a+b; //addition
    printf("a+b is %d\n", res);
    res = a-b; //subtraction
    printf("a-b is %d\n", res);
    res = a*b; //multiplication
    printf("a*b is %d\n", res);
    res = a/b; //division
    printf("a/b is %d\n", res);
    res = a%b; //modulus
    printf("a%%b is %d\n", res);
    return 0;
}

输出:

a是10,b是4
a + b是14
ab是6
a * b是40
a / b是2
a%b为2

落入一元算术运算符类别的是:

  • 增量:“++”操作符用于递增的整数的值。当放置在变量名称之前(也称为预增量运算符)时,其值立即递增。例如,++ x
    当它放在变量名之后(也称为后增量运算符)时,它的值会暂时保留,直到执行此语句并在执行下一个语句之前更新它。例如,x ++
  • 递减:“ - ”操作符用于递减的整数的值。当放置在变量名称之前(也称为预减量运算符)时,其值立即递减。例如,-x
    当它放在变量名称之后(也称为后递减运算符)时,它的值会暂时保留,直到执行此语句并在执行下一个语句之前更新它。例如,x-
// C program to demonstrate working of Unary arithmetic operators
#include<stdio.h>
int main()
{
    int a = 10, b = 4, res;
    // post-increment example:
    // res is assigned 10 only, a is not updated yet
    res = a++;
    printf("a is %d and res is %d\n", a, res); //a becomes 11 now
    // post-decrement example:
    // res is assigned 11 only, a is not updated yet
    res = a--;
    printf("a is %d and res is %d\n", a, res);  //a becomes 10 now
    // pre-increment example:
    // res is assigned 11 now since a is updated here itself
    res = ++a;
    // a and res have same values = 11
    printf("a is %d and res is %d\n", a, res);
    // pre-decrement example:
    // res is assigned 10 only since a is updated here itself
    res = --a;
    // a and res have same values = 10
    printf("a is %d and res is %d\n",a,res);
    return 0;
}

输出:

a是11,res是10
a是10,res是11
a是11,res是11
a是10,res是10

我们很快将讨论不同职位的其他类别的运营商。

C中的运算符 设置1(算术运算符)-IDC帮帮忙