# C 编程运算符 > 原文: [https://www.programiz.com/c-programming/c-operators](https://www.programiz.com/c-programming/c-operators) #### 在本教程中,您将借助示例学习 C 编程中的不同运算符。 运算符是对值或变量进行运算的符号。 例如:`+`是执行加法的运算符。 C 具有广泛的运算符来执行各种操作。 * * * ## C 算术运算符 算术运算符对数值(常数和变量)执行数学运算,例如加,减,乘,除等。 | 操作员 | 运算符的含义 | | --- | --- | | + | 一加或一加 | | - | 减法或一元减 | | * | 乘法 | | / | 师 | | % | 除法后的余数(模除) | ### 示例 1:算术运算符 ```c // 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; } ``` **输出** ```c 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.0`,`b = 2.0`,`c = 5`和`d = 2`。 然后在 C 程式设计中 ```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:递增和递减运算符 ```c // 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; } ``` **输出** ```c ++a = 11 --b = 99 ++c = 11.500000 ++d = 99.500000 ``` 在此,将运算符`++`和`--`用作前缀。 这两个运算符还可以用作`a++`和`a--`之类的后缀。 访问此页面以了解有关[递增和递减运算符用作后缀](/article/increment-decrement-operator-difference-prefix-postfix "Increment Operator as postfix")时如何工作的更多信息。 * * * ## C 赋值运算符 赋值运算符用于为变量赋值。 最常见的赋值运算符是`=` | 运算符 | 示例 | 和...一样 | | --- | --- | --- | | `=` | `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:赋值运算符 ```c // 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 c = 5 c = 10 c = 5 c = 25 c = 5 c = 0 ``` * * * ### C 关系运算符 关系运算符检查两个操作数之间的关系。 如果该关系为真,则返回 1;否则为 0。 如果该关系为假,则返回值 0。 关系运算符用于[决策](/c-programming/c-if-else-statement "C if else")和[循环](/c-programming/c-for-loop "C for loop")中。 | 运算符 | Meaning of Operator | Example | | --- | --- | --- | | `==` | 等于 | `5 == 3`求值为 0 | | `>` | 大于 | `5 > 3`求值为 1 | | `<` | 小于 | `5 < 3`求值为 0 | | `!=` | 不等于 | `5 != 3`求值为 1 | | `>=` | 大于或等于 | `5 >= 3`求值为 1 | | `<=` | 小于或等于 | `5 <= 3`求值为 0 | ### 示例 4:关系运算符 ```c // 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; } ``` **输出** ```c 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 逻辑运算符 包含逻辑运算符的表达式返回 0 或 1,具体取决于表达式结果为`true`还是`false`。 逻辑运算符通常用于 C 编程的[决策中](/c-programming/c-if-else-statement "C if else")。 | 运算符 | 含义 | Example | | --- | --- | --- | | `&&` | 逻辑与。 仅当所有操作数均为真时为真 | 如果`c = 5`且`d = 2`,则表达式`((c==5) && (d>5))`等于 0。 | | || | 逻辑或。 仅当任一操作数为真时为真 | 如果`c = 5`且`d = 2`,则表达式`((c==5) || (d>5))`等于 1。 | | `!` | 逻辑非。 仅当操作数为 0 时为真 | 如果`c = 5`,则表达式`!(c==5)`等于 0。 | ### 示例 5:逻辑运算符 ```c // 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; } ``` **输出** ```c (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(真)。 * `(a == b) && (c < b)`的值为 0,因为操作数`(c < b)`为 0(假)。 * `(a == b) || (c < b)`的值为 1,因为`(a = b)`为 1(真)。 * `(a != b) || (c < b)`的值为 0,因为操作数`(a != b)`和`(c < b)`均为 0(假)。 * `!(a != b)`的值为 1,因为操作数`(a != b)`为 0(假)。 因此,`!(a != b)`为 1(真)。 * `!(a == b)`的值为 0,因为`(a == b)`为 1(真)。 因此,`!(a == b)`为 0(假)。 * * * ### C 按位运算符 在计算过程中,数学运算(如加,减,乘,除等)被转换为比特级,从而加快了处理速度并节省了电能。 在 C 编程中使用按位运算符执行位级操作。 | 经营者 | 运算符的含义 | | --- | --- | | & | 按位与 | | | | 按位或 | | ^ | 按位异或 | | ~ | 按位补码 | | < < | 左移 | | > > | 右移 | 访问 C 中的[按位运算符以了解更多信息](/c-programming/bitwise-operators "C bitwise operators")。 ## 其他运算符 * * * ### 逗号运算符 逗号运算符用于将相关表达式链接在一起。 例如: ```c int a, c = 5, d; ``` * * * ### 大小运算符 `sizeof`是一元运算符,它返回数据的大小(常量,变量,数组,结构等)。 ### 示例 6:`sizeof`运算符 ```c #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; } ``` **输出** ```c Size of int = 4 bytes Size of float = 4 bytes Size of double = 8 bytes Size of char = 1 byte ``` * * * 三元运算符`?:`,引用运算符`&`,取消引用运算符`*`和成员选择运算符`->`等其他运算符将在以后的教程中进行讨论。