Logical Operators - C Operator

C examples for Operator:Logic Operator

Introduction

Logical and operator && evaluates to true if both the left and right sides are true.

Logical or || is true if either the left or right side is true.

To invert a Boolean result, use the logical not ! operator.

For both "logical and" and "logical or" operator, the right-hand side will not be evaluated if the result can be determined by the left-hand side.

Demo Code

#include <stdio.h>
int main(void) {

    int x = (1 && 0); /* 0 - logical and */
        x = (1 || 0); /* 1 - logical or  */
        x = !(1);     /* 0 - logical not */
}

<stdbool.h> defines the constants true and false to represent 1 and 0.

Demo Code

#include <stdbool.h>
#include <stdio.h>
int main(void) {

    bool x = (true && false); /* false - logical and */
         x = (true || false); /* true  - logical or  */
         x = !(true);         /* false - logical not */
}

Related Tutorials