Explain the concept of logical and assignment operator in C language


First, let us learn about the logical operator.

Logical operator

  • These are used to combine 2 (or) more expressions logically.

  • They are logical AND (&&) logical OR ( || ) and logical NOT (!)

Logical AND (&&)

exp1exp2exp1&&exp2
TTT
TFF
FTF
FFF


Logical OR(||)

exp1exp2exp1||exp2
TTT
TFT
FTT
FFF


Logical NOT(!)

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

Example

Following is the C program to compute logical operators −

 Live Demo

#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// }

Output

You will see the following output −

0
1
0
0
0
1

Assignment Operator

It is used to assign a value to a variable.

Types

The types of assignment operators are −

  • Simple assignment
  • Compound assignment
OperatorDescriptionExample
=simple assignmenta=10
+=,-=,*=,/=,%=compound assignmenta+=10"a=a+10
a=10"a=a-10

Program

Given below is the C program for compound assignment operator −

 Live Demo

#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; }

Output

You will see the following output −

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

Updated on: 15-Mar-2021

323 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements