📜  C编程运算符

📅  最后修改于: 2020-10-04 12:20:36             🧑  作者: Mango

在本教程中,您将借助示例来学习C编程中的不同运算符 。

运算符是对值或变量进行运算的符号。例如: +是执行加法的运算符 。

C具有广泛的运算符来执行各种操作。


C算术运算符

算术运算运算符对数值(常数和变量)执行数学运算,例如加,减,乘,除等。

Operator Meaning of Operator
+ addition or unary plus
subtraction or unary minus
* multiplication
/ division
% remainder after division (modulo division)

示例1:算术运算符

// Working of arithmetic operators
#include 
int main()
{
    int a = 9,b = 4, c;
    
    c = a+b;
    printf("a+b = %d \n",c);
    c = a-b;
    printf("a-b = %d \n",c);
    c = a*b;
    printf("a*b = %d \n",c);
    c = a/b;
    printf("a/b = %d \n",c);
    c = a%b;
    printf("Remainder when a divided by b = %d \n",c);
    
    return 0;
}

输出

a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1

如您所料,运算运算符 +-*计算加法,减法和乘法。

在正常计算中, 9/4 = 2.25 。但是,程序中的输出为2

这是因为变量a和b都是整数。因此,输出也是整数。编译器忽略小数点后的术语,并显示答案2而不是2.25

模运算符 %计算余数。当a=9除以b=4 ,余数为1% 运算符只能与整数一起使用。

假设a = 5.0b = 2.0c = 5d = 2 。然后在C程式设计中

// Either one of the operands is a floating-point number
a/b = 2.5  
a/d = 2.5  
c/b = 2.5  

// Both operands are integers
c/d = 2

C增减运算符

C编程具有两个运算符递增++和递减--将操作数(常量或变量)的值更改为1。

增量++加1的值,而减量--减小1。这两个运算符是一元运算运算符的值,这意味着它们只能在单个操作数操作。

示例2:增量和减量运算符

// Working of increment and decrement operators
#include 
int main()
{
    int a = 10, b = 100;
    float c = 10.5, d = 100.5;

    printf("++a = %d \n", ++a);
    printf("--b = %d \n", --b);
    printf("++c = %f \n", ++c);
    printf("--d = %f \n", --d);

    return 0;
}

输出

++a = 11
--b = 99
++c = 11.500000
--d = 99.500000

在这里, 运算符 ++--用作前缀。这两个运算符也可以用作a++a--a++后缀。访问此页面以了解有关用作后缀的增量和减量运算符如何工作的更多信息。


C赋值运算符

赋值运算符用于为变量赋值。最常见的赋值运算符是=

Operator Example Same as
= a = b a = b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b

示例3:赋值运算符

// Working of assignment operators
#include 
int main()
{
    int a = 5, c;

    c = a;      // c is 5
    printf("c = %d\n", c);
    c += a;     // c is 10 
    printf("c = %d\n", c);
    c -= a;     // c is 5
    printf("c = %d\n", c);
    c *= a;     // c is 25
    printf("c = %d\n", c);
    c /= a;     // c is 5
    printf("c = %d\n", c);
    c %= a;     // c = 0
    printf("c = %d\n", c);

    return 0;
}

输出

c = 5 
c = 10 
c = 5 
c = 25 
c = 5 
c = 0

C关系运算符

关系运算符检查两个操作数之间的关系。如果该关系为真,则返回1;否则为0。如果该关系为假,则返回值0。

关系运算符用于决策和循环。

Operator Meaning of Operator Example
== Equal to 5 == 3 is evaluated to 0
> Greater than 5 > 3 is evaluated to 1
< Less than 5 < 3 is evaluated to 0
!= Not equal to 5 != 3 is evaluated to 1
>= Greater than or equal to 5 >= 3 is evaluated to 1
<= Less than or equal to 5 <= 3 is evaluated to 0

示例4:关系运算符

// Working of relational operators
#include 
int main()
{
    int a = 5, b = 5, c = 10;

    printf("%d == %d is %d \n", a, b, a == b);
    printf("%d == %d is %d \n", a, c, a == c);
    printf("%d > %d is %d \n", a, b, a > b);
    printf("%d > %d is %d \n", a, c, a > c);
    printf("%d < %d is %d \n", a, b, a < b);
    printf("%d < %d is %d \n", a, c, a < c);
    printf("%d != %d is %d \n", a, b, a != b);
    printf("%d != %d is %d \n", a, c, a != c);
    printf("%d >= %d is %d \n", a, b, a >= b);
    printf("%d >= %d is %d \n", a, c, a >= c);
    printf("%d <= %d is %d \n", a, b, a <= b);
    printf("%d <= %d is %d \n", a, c, a <= c);

    return 0;
}

输出

5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1 

C逻辑运算符

包含逻辑运算符的表达式将根据表达式的结果是true还是false返回0或1。逻辑运算符通常用于C编程的决策中。

Operator Meaning Example
&& Logical AND. True only if all operands are true If c = 5 and d = 2 then, expression ((c==5) && (d>5)) equals to 0.
|| Logical OR. True only if either one operand is true If c = 5 and d = 2 then, expression ((c==5) || (d>5)) equals to 1.
! Logical NOT. True only if the operand is 0 If c = 5 then, expression !(c==5) equals to 0.

示例5:逻辑运算符

// Working of logical operators

#include 
int main()
{
    int a = 5, b = 5, c = 10, result;

    result = (a == b) && (c > b);
    printf("(a == b) && (c > b) is %d \n", result);

    result = (a == b) && (c < b);
    printf("(a == b) && (c < b) is %d \n", result);

    result = (a == b) || (c < b);
    printf("(a == b) || (c < b) is %d \n", result);

    result = (a != b) || (c < b);
    printf("(a != b) || (c < b) is %d \n", result);

    result = !(a != b);
    printf("!(a != b) is %d \n", result);

    result = !(a == b);
    printf("!(a == b) is %d \n", result);

    return 0;
}

输出

(a == b) && (c > b) is 1 
(a == b) && (c < b) is 0 
(a == b) || (c < b) is 1 
(a != b) || (c < b) is 0 
!(a != b) is 1 
!(a == b) is 0 

逻辑运算符程序的说明

  • (a == b) && (c > 5)求值为1,因为两个操作数(a == b)(c > b)均为1(true)。
  • (a == b) && (c < b)求值为0,因为操作数(c < b)为0(false)。
  • (a == b) || (c < b)值为1,因为(a = b)为1(真)。
  • (a != b) || (c < b)值为0,因为操作数(a != b)(c < b)均为0(false)。
  • !(a != b)求值为1,因为操作数(a != b)为0(假)。因此,!(a!= b)为1(真)。
  • !(a == b)值为0,因为(a == b)为1(真)。因此, !(a == b)为0(假)。

C按位运算符

在计算过程中,数学运算(如加,减,乘,除等)被转换为比特级,从而加快了处理速度并节省了电能。

在C编程中,按位运算符用于执行位级操作。

Operators Meaning of operators
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right

访问C中的按位运算运算符以了解更多信息。

其他运营商


逗号运算符

逗号运算符用于将相关表达式链接在一起。例如:

int a, c = 5, d;

sizeof 运算符

sizeof是一元运算运算符 ,返回数据的大小(常量,变量,数组,结构等)。

示例6:sizeof运算符

#include 
int main()
{
    int a;
    float b;
    double c;
    char d;
    printf("Size of int=%lu bytes\n",sizeof(a));
    printf("Size of float=%lu bytes\n",sizeof(b));
    printf("Size of double=%lu bytes\n",sizeof(c));
    printf("Size of char=%lu byte\n",sizeof(d));

    return 0;
}

输出

Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte

其他运算符,例如三元运算符 ?: ,引用运算符 & ,取消引用运算符 *和成员选择运算符 ->将在以后的教程中进行讨论。