How to use the C conditional operator to simplify the syntax of the relational expression

 

 

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: Using the C conditional operator to simplify the syntax of the relational expression

To show: How to use C conditional operator to simplify the syntax of the relational expression

 

 

#include <stdio.h>

 

int main(void)

{

int a, b = 4, c= 50;

 

// Here b is less than c, so the statement, (b>c) is false, then 200 should be assigned to a, reflected through the output

a = (b>c) ? 100 : 200;

 

printf("Given: b = 4, c= 50\n");

printf("The statement:a = (b>c) ? 100 : 200\n");

printf("will be evaluated to a = %d\n", a);

return 0;

}

 

Output example:

 

Given: b = 4, c= 50

The statement:a = (b>c) ? 100 : 200

will be evaluated to a = 200

Press any key to continue . . .

 

 

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