# Logical operators in c

Using logical operators we can check multiple conditions .this condition has two answers true or false ,for example “3 is odd or even number” the answer is true .there are so many multiple conditions in logical operators .these are shown in table 2

Operator | Meaning | Example |
---|---|---|

&& | Logical AND | If c=1 and d=2 then, (c==1) && (d>1) equals to 0 |

|| | Logical OR | If c=1 and d=2 then, (c==1) || (d>1) equals to 1. |

! | Logical NOT | If c =1 then, expression ! (c == 1) equals to 0. |

if there are two conditions .the logical operators works as shown in below table

logical operator | first condition | second condition | result |
---|---|---|---|

&& | true | true | true |

&& | true | false | false |

&& | false | true | false |

&& | false | false | false |

|| | true | true | true |

|| | true | false | true |

|| | false | true | true |

|| | false | false | false |

example:

#include <stdio.h>

int main()

{

int a = 5, b = 5, c = 10, result;

result = (a = b) && (c > b);

printf("(a = b) && (c > b) equals to %d \n", result);

result = (a = b) && (c < b);

printf("(a = b) && (c < b) equals to %d \n", result);

result = (a = b) || (c < b);

printf("(a = b) || (c < b) equals to %d \n", result);

result = (a != b) || (c < b);

printf("(a != b) || (c < b) equals to %d \n", result);

result = !(a != b);

printf("!(a == b) equals to %d \n", result);

result = !(a == b);

printf("!(a == b) equals to %d \n", result);

return 0;

}

(a = b) && (c > b) equals to 1

(a = b) && (c < b) equals to 0

(a = b) || (c < b) equals to 1

(a != b) || (c < b) equals to 0

!(a == b) equals to 1

!(a == b) equals to 0

Press any key to continue . . .