在本章中,我们将学习以下主题:
算术运算符。
下面是算术运算符的列表:
Operator Operator Name Example + Addition 3 + 3 = 6 - Subtraction 5 - 2 = 3 * Multiplication 3 * 3 = 9 / Division 9 / 3 = 3 % Modulus 15 % 2 = 1 [remainder]
演示算术运算符的程序:
#include<stdio.h> int main() { int num_1 = 20; int num_2 = 10; printf("The addition of %d and %d is = %d \n", num_1, num_2, (num_1 + num_2) ); printf("The Subtraction of %d and %d is = %d \n", num_1, num_2, (num_1 - num_2) ); printf("The Multiplication of %d and %d is = %d \n", num_1, num_2, (num_1 * num_2) ); printf("The Division of %d and %d is = %d \n", num_1, num_2, (num_1 / num_2) ); printf("The Modulus of %d and %d is = %d \n", num_1, num_2, (num_1 % num_2) ); return 0; }
输出:
The addition of 20 and 10 is = 30 The Subtraction of 20 and 10 is = 10 The Multiplication of 20 and 10 is = 200 The Division of 20 and 10 is = 2 The Modulus of 20 and 10 is = 0
分配运算符。
赋值运算符可用于为变量赋值。有一些速记赋值运算符。就是说,如果您想将值10加到变量“ a”,那么我们通常会写“ a = a + 10”。取而代之的是,我们可以写“ a + = 10”。
以下是赋值运算符的列表:
Operator Example = a = 10; += a += 10; or a = a + 10; *= a *= 10; or a = a * 10; /= a /= 10; or a = a / 10; %= a %= 10; or a = a % 10; -= a -= 10; or a = a - 10; <<= a <<= 10; or a = a << 10; >>= a >>= 10; or a = a >> 10; >>>= a >>>= 10; or a = a >>> 10; &= a &= 10; or a = a & 10; ^= a ^= 10; or a = a ^ 10; != a != 10; or a = a ! 10;
以下是赋值运算符的示例:
#include<stdio.h> int main() { int num_1 = 10; printf("The result of += operator is = %d\n", num_1+=10 ); printf("The result of *= operator is = %d\n", num_1*=10 ); printf("The result of /= operator is = %d\n", num_1/=10 ); printf("The result of %= operator is = %d\n", num_1%=10 ); printf("The result of -= operator is = %d\n", num_1-=10 ); printf("The result of <<= operator is = %d\n", num_1<<=10 ); printf("The result of >>= operator is = %d\n", num_1>>=10 ); printf("The result of &= operator is = %d\n", num_1&=10 ); printf("The result of ^= operator is = %d\n", num_1^=10 ); printf("The result of != operator is = %d\n", num_1!=10 ); return 0; }
输出:
The result of += operator is = 20 The result of *= operator is = 200 The result of /= operator is = 20 The result of = operator is = 0 The result of -= operator is = -10 The result of <<= operator is = -10240 The result of >>= operator is = -10 The result of &= operator is = 2 The result of ^= operator is = 8 The result of != operator is = 1
关系运算符:
关系运算符用于检查两个值是true还是false。
如果表达式返回0,则为false;如果表达式返回1,则为true。
以下是关系运算符及其结果的列表。
Assume a = 10, b = 20
Operator Operator Name Example Result > Greater Than a > b 0 < Lesser Than a < b 1 >= Greater than or equal to a >= b 0 <= Lesser than or equal to a <= b 1 == Equal to a == b 0 != Not equal to a != b 1
以下是关系运算符的示例:
#include<stdio.h> int main() { int num_1 = 20; int num_2 = 10; printf("The result of %d > %d operator is = %d\n", num_1, num_2, (num_1 > num_2) ); printf("The result of %d < %d operator is = %d\n", num_1, num_2, (num_1 < num_2) ); printf("The result of %d >= %d operator is = %d\n", num_1, num_2, (num_1 >= num_2) ); printf("The result of %d <= %d operator is = %d\n", num_1, num_2, (num_1 <= num_2) ); printf("The result of %d == %d operator is = %d\n", num_1, num_2, (num_1 == num_2) ); printf("The result of %d != %d operator is = %d\n", num_1, num_2, (num_1 != num_2) ); return 0; }
输出:
The result of 20 > 10 operator is = 1 The result of 20 < 10 operator is = 0 The result of 20 >= 10 operator is = 1 The result of 20 <= 10 operator is = 0 The result of 20 == 10 operator is = 0 The result of 20 != 10 operator is = 1
逻辑运算符。
逻辑运算符用于检查两个表达式之间的关系是对还是错。
以下是不同类型的逻辑运算符:
Assume a = 10, b = 20
Operator Operator Name Example Result && Logical AND 10 > 5 && 6 >3 1 || Logical OR 10 > 5 && 6 <3 1 ! Logical NOT 10 != 10 0
在 逻辑与 如果两个表达式都为真,则将返回True。如果任何一个表达式为假,则它将返回假。
在 逻辑或 如果任何一个表达式为true,则将返回True。如果两个表达式都为假,则它将返回假。
在 逻辑非 如果表达式为假,将返回True,反之亦然。
以下是示例 关系的 操作员:
#include<stdio.h> int main() { int num_1 = 20; int num_2 = 10; printf("The result of (num_1 > 30) && (num_2 > 5) operator is = %d\n", (num_1 > 30) && (num_2 > 5) ); printf("The result of (num_1 > 30) || (num_2 > 5) operator is = %d\n", (num_1 > 30) || (num_2 > 5) ); return 0; }
输出:
The result of (num_1 > 30) && (num_2 > 5) operator is = 0 The result of (num_1 > 30) || (num_2 > 5) operator is = 1
按位运算符:
按位运算符用于位操作。它们只能应用于int,char,short和long数据类型。
以下是按位运算符的列表:
Operator Operator Name >> Right shift << Left Shift ^ Bitwise XOR ~ One’s complement & Bitwise AND | Bitwise OR
下面是按位运算符的示例:
#include<stdio.h> int main() { int num_1 = 20; int num_2 = 10; printf("The result of %d>> 2 is = %d\n", num_1, num_1>>2 ); printf("The result of %d << 2 is = %d\n", num_1, num_1<<2 ); printf("The result of %d & %d is = %d\n", num_1, num_2, num_1 & num_2 ); printf("The result of %d | %d is = %d\n", num_1, num_2, num_1 | num_2 ); printf("The result of %d ^ %d is = %d\n", num_1, num_2, num_1 ^ num_2 ); return 0; }
输出:
The result of 20>> 2 is = 5 The result of 20 << 2 is = 80 The result of 20 & 10 is = 0 The result of 20 | 10 is = 30 The result of 20 ^ 10 is = 30
条件运算符:
条件运算符是if-else语句的简写。它们也称为三元运算符。
该运算符包括3个部分。条件部分后跟2个表达式或值,如果条件为true,则首先执行,如果条件为false,则首先执行。
句法:
Condition : (expression to be executed if true) ? (expression to be executed if false);
例:
#include<stdio.h> int main(int argc, char const *argv[]) { printf("is 4 > 5 ? = %s \n", ( (4 > 5) ? "True" : "False")); return 0; }
输出:
is 4 > 5 ? = False
增量和减量运算符。
它们也称为一元运算符。因为它们仅对一个操作数进行运算。
++是增量运算符
—是减量运算符
++ num是预递增的。此处的值将递增,然后分配给num。
num ++是后递增的。此处将分配值,然后将其递增。
下面是相同的示例:
#include<stdio.h> int main() { int num = 1; printf("performing pre increment ++%d = %d \n",num, ++num ); //as we have incremented the value above from 1 to 2, here the num value will be 2 printf("performing post increment %d++ = %d \n",num, num++ ); //as we have incremented the value above from 2 to 3, here the num value will be 3. printf("performing pre decrement --%d = %d \n",num, --num ); //as we have decremented the value above from 3 to 2, here the num value will be 2. printf("performing post decrement %d-- = %d \n",num, num-- ); return 0; }
输出:
performing pre increment ++1 = 2 performing post increment 2++ = 2 performing pre decrement --3 = 2 performing post decrement 2-- = 2
该网站上可用的教程列表:
C编程20+章 | C ++编程80+章 |
100多个编码问题 | 数据结构和算法85+章 |
系统设计20+章 | Shell脚本编写12章 |
4g LTE 60+章节 | 最常见的编码问题 |
5G NR 50+章 | Linux系统编程20+章 |