1

C Relational Operators


The 6 relational operators in C are:

  • == equals (= is used as variable assignment, == is used as comparison between values)
  • != does not equal
  • > is greater than
  • < is less then
  • >= is greater than or equal to
  • <= is less than or equal to

Here is the sample code for the relational operators:

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

#include <stdio.h>

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

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

 if (age <= 19) {
   if (age >= 13)
     printf("You are a teenagern");
   else
     printf("You are just a kidn");
 }
 else
   printf("My, you are very old!n");

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

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

 ...    // To be continued
}

Notice that there is a new string function. strcmp() function is used to compare two strings. The strings are the same if this function returns 0, otherwise it returns either less than 0 or greater than 0 depending on the first letter of the string.

There are no posts related to C Relational Operators.

  1. [...] 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 [...]