Operators in C | Logical operators

Example of AND(&&) Operator

Helps us to combine two or more conditions, the result is True(1) only when all the conditions are Ture otherwise the result is False(0).

#include<stdio.h>
int main()
{
    int a=10,b=20,c;
    c=a && b;
    printf("a = %d\n",a);
    printf("b = %d\n",b);
    printf("%d && %d = %d\n",a,b,c);
    return 0;
}
/* Output */
a = 10
b = 20
10 && 20 = 1
#include<stdio.h>
int main()
{
    int a=10,b=0,c;
    c=a && b;
    printf("a = %d\n",a);
    printf("b = %d\n",b);
    printf("%d && %d = %d\n",a,b,c);
    return 0;
}
/* Output */
a = 10
b = 0
10 && 0 = 0

Example of OR(||) Operator

Helps us to combine two or more conditions, the result is True(1) when any one of the conditions is Ture, the result is False(0) only when all the conditions are False.

#include<stdio.h>
int main()
{
    int a=10,b=20,c;
    c=a || b;
    printf("a = %d\n",a);
    printf("b = %d\n",b);
    printf("%d || %d = %d\n",a,b,c);
    return 0;
}
/* Output */
a = 10
b = 20
10 || 20 = 1
#include<stdio.h>
int main()
{
    int a=10,b=0,c;
    c=a || b;
    printf("a = %d\n",a);
    printf("b = %d\n",b);
    printf("%d || %d = %d\n",a,b,c);
    return 0;
}
/* Output */
a = 10
b = 0
10 || 0 = 1
#include<stdio.h>
int main()
{
    int a=0,b=0,c;
    c=a || b;
    printf("a = %d\n",a);
    printf("b = %d\n",b);
    printf("%d || %d = %d\n",a,b,c);
    return 0;
}
/* Output */
a = 0
b = 0
0 || 0 = 0

Example of NOT(!) Operator

It is a negation operator if the condition is True(1) result becomes False(0) and vice versa.

#include<stdio.h>
int main()
{
    int a=10,b;
    b=!a;
    printf("a = %d\n",a);
    printf("!%d  = %d\n",a,b);
    return 0;
}
/* Output */
a = 10
!10  = 0
#include<stdio.h>
int main()
{
    int a=0,b;
    b=!a;
    printf("a = %d\n",a);
    printf("!%d  = %d\n",a,b);
    return 0;
}
/* Output */
a = 0
!0  = 1