0

C Advanced Relational Operators


The following are the advanced relational operators:

  • && and
  • || or
  • ! not

(The precedence is very important while using these operators.)

Here is the sample code for the advanced relational operators:

/*
 * age4.c
 *
 * Demonstrate the use of the various relational operators
 * AND
 * Demonstrate the concept of advanced relational operators
 *
 * by Mark Virtue, 2001.
 *
 */

#include <stdio.h>

main() {
 int age;
 char name[41];
 int very_old;

 printf("Please enter your age: ");
 scanf("%d", &age);
 printf("You are %d years oldn", age);

 if (!(age <= 19 && age >= 13))    // this condition is false
   printf("You are not a teenagern");

 if (age > 19 || age < 13)    // It is the same as above conditional statement
   printf("You are not a teenagern");

 if (age == 10 || age == 20 || age == 30 && age > 100)    // Always false
   printf("You have a special agen");

 printf("Please enter your name: ");
 scanf("%s", name);

 if (!(strcmp(name, "Bruce") != 0 && age != 40))
   printf("You are called Bruce. Do you mind if I call you Bruce?n");

 if (strcmp(name, "Bruce") != 0 || age == 40))
   printf("You are not called Bruce. Do you mind if I call you Bruce?n");

 very_old = age > 80;

 if (!very_old)
   printf("You are not very oldn");

 if (very_old == 0)    // same as the last statement
   printf("You are not very oldn");

 fflush(stdin);
 getchar();
}

Note that the code above is the continuation of the basic relational operators. The not(!) operator reverses a conditional statement. It can make true conditions false and false conditions true.

There are no posts related to C Advanced Relational Operators.