解释 C 语言中逻辑和赋值运算符的概念

cserver side programmingprogramming

首先,让我们了解一下逻辑运算符。

逻辑运算符

  • 它们用于逻辑地组合 2 个(或)多个表达式。

  • 它们是逻辑与 (&&)、逻辑或 ( || ) 和逻辑非 (!)

逻辑与 (&&)

exp1exp2exp1&&exp2
TTT
TFF
FTF
FFF

逻辑或(||)

exp1exp2exp1||exp2
TTT
TFT
FTT
FFF

逻辑非(!)

exp!exp
TT
FT
OperatorDescriptionExamplea=10,b=20,c=30Output
&&logical AND(a>b)&&(a<c)(10>20)&&(10<30)0
||logical OR(a>b)||(a<=c)(10>20)||(10<30)1
!logical NOT!(a>b)!(10>20)1

示例

以下是计算逻辑运算符的 C 程序 −

#include<stdio.h>
main (){
   float a=0.5,b=0.3,c=0.7;
   printf("%d
",(a<b)&&(b>c));//0//    printf("%d
",(a>=b)&&(b<=c));//1//    printf("%d
",(a==b)||(b==c));//0//    printf("%d
",(b>=a)||(a==c));//0//    printf("%d
",(b<=c)&&!(c>=a));//0//    printf("%d
",!(b<=c)||(c>=a));//1// }

输出

您将看到以下输出 −

0
1
0
0
0
1

赋值运算符

用于为变量赋值。

类型

赋值运算符的类型有 −

  • 简单赋值
  • 复合赋值
运算符描述示例
=简单赋值a=10
+=,-=,*=,/=,%=复合赋值a+=10"a=a+10
a=10"a=a-10

程序

下面给出的是复合赋值运算符的 C 程序 −

#include<stdio.h>
int main(void){
   int i;
   char a='h';
   printf("enter the value of i:
");    scanf("%d",&i);    printf("print ASCII value of %c is %d
", a, a);    a += 5;    printf("print ASCII value of %c is %d
", a, a);    a *= a + i;    printf("a = %d
", a);    a *= 3;    printf("a = %d
", a);    a /= 2;    printf("a = %d
", a);    a %= 4;    printf("a = %d
", a);    return 0; }

输出

您将看到以下输出 −

enter the value of i:
3
print ASCII value of h is 104
print ASCII value of m is 109
a = -80
a = 16
a = 8
a = 0

相关文章