The C relational operators and integers comparison with operator precedence program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: none

To do: How to compare integers using operator precedence and relational operators

To show: The C relational operators and integers comparison with operator precedence program example

 

 

#include <stdio.h>

int main(void)

{

// Initialize variables and note that c is not less than d, which is one of the conditions to test for

// therefore the entire expression should be evaluated as false

int a = 5, b = 6, c = 5, d = 1;

int x;

 

// Evaluate the expression without parentheses

x = a < b || a < c && c < d;

printf("Given expression:\n");

printf("x = a < b || a < c && c < d\n");

printf("Without parentheses the expression evaluates as %d", x);

 

// Evaluate the expression with parentheses

x = (a < b || a < c) && (c < d);

printf("\n\nWith parentheses:\n");

printf("x = (a < b || a < c) && (c < d)\n");

printf("With parentheses the expression evaluates as %d\n", x);

 

return 0;

}

 

Output example:

 

Given expression:

x = a < b || a < c && c < d

Without parentheses the expression evaluates as 1

With parentheses:

x = (a < b || a < c) && (c < d)

With parentheses the expression evaluates as 0

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index