Relational and logical operators - C Operator

C examples for Operator:Relational Operator

Introduction

Relational Operators

Operator Action
>Greater than
>= Greater than or equal
<Less than
<= Less than or equal
== Equal
!= Not equal

Logical Operators

Operator Action
&& AND
|| OR
!NOT

The following shows the relational and logical operators.

The truth table for the logical operators is shown here using 1's and 0's.

p q p && q p || q !p
0 0 0 0 1
0 1 0 1 1
1 1 1 1 0
1 0 0 1 0

C does not contain an exclusive OR (XOR) logical operator, the following code is trying to create a function for XOR.

The outcome of an XOR operation is true if and only if one operand (but not both) is true.

Demo Code

#include <stdio.h>

int xorf(int a, int b);

int main(void)
{
   printf("%d", xorf (1, 0));
   printf("%d", xorf (1, 1));
   printf("%d", xorf (0, 1));
   printf("%d", xorf (0, 0));

   return 0;//from  w ww . ja va2 s .c  o m
}

/* Perform a logical XOR operation using the
two arguments. */
int xorf(int a, int b)
{
   return (a || b) && !(a && b);
}

Result

The following table shows the relative precedence of the relational and logical operators:

Highest                       !
                              > >= < <=
                              == !=
                              &&
Lowest                        ||

Related Tutorials