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 (&&)
| exp1 | exp2 | exp1&&exp2 |
|---|---|---|
| T | T | T |
| T | F | F |
| F | T | F |
| F | F | F |
Logical OR(||)
| exp1 | exp2 | exp1||exp2 |
|---|---|---|
| T | T | T |
| T | F | T |
| F | T | T |
| F | F | F |
Logical NOT(!)
| exp | !exp |
|---|---|
| T | T |
| F | T |
| Operator | Description | Example | a=10,b=20,c=30 | Output |
|---|---|---|---|---|
| && | 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 −
#include<stdio.h>
main (){
float a=0.5,b=0.3,c=0.7;
printf("%d\n",(a<b)&&(b>c));//0//
printf("%d\n",(a>=b)&&(b<=c));//1//
printf("%d\n",(a==b)||(b==c));//0//
printf("%d\n",(b>=a)||(a==c));//0//
printf("%d\n",(b<=c)&&!(c>=a));//0//
printf("%d\n",!(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
| Operator | Description | Example |
|---|---|---|
| = | simple assignment | a=10 |
| +=,-=,*=,/=,%= | compound assignment | a+=10"a=a+10 a=10"a=a-10 |
Program
Given below is the C program for compound assignment operator −
#include<stdio.h>
int main(void){
int i;
char a='h';
printf("enter the value of i:\n");
scanf("%d",&i);
printf("print ASCII value of %c is %d\n", a, a);
a += 5;
printf("print ASCII value of %c is %d\n", a, a);
a *= a + i;
printf("a = %d\n", a);
a *= 3;
printf("a = %d\n", a);
a /= 2;
printf("a = %d\n", a);
a %= 4;
printf("a = %d\n", 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