Relational operators in C programming language are as follows: <, <=, >, >=, ==, != . They are used in Boolean conditions or expression and returns true or false. Here is a program which demonstrate relational operators:
01 | #include <stdio.h> |
02 | /* a program demonstrates C relational operators */ |
03 | |
04 | |
05 | void print_bool( bool value){ |
06 | value == true ? printf ( "true\n" ) : printf ( "false\n" ); |
07 | }; |
08 | |
09 | void main(){ |
10 | int x = 10, y = 20; |
11 | |
12 | printf ( "x = %d\n" ,x); |
13 | printf ( "y = %d\n" ,y); |
14 | |
15 | /* demonstrate == operator */ |
16 | bool result = (x == y); |
17 | printf ( "bool result = (x == y);" ); |
18 | print_bool(result); |
19 | |
20 | /* demonstrate != operator */ |
21 | result = (x != y); |
22 | printf ( "bool result = (x != y);" ); |
23 | print_bool(result); |
24 | |
25 | /* demonstrate > operator */ |
26 | result = (x > y); |
27 | printf ( "bool result = (x > y);" ); |
28 | print_bool(result); |
29 | |
30 | /* demonstrate >= operator */ |
31 | result = (x >= y); |
32 | printf ( "bool result = (x >= y);" ); |
33 | print_bool(result); |
34 | |
35 | /* demonstrate < operator */ |
36 | result = (x < y); |
37 | printf ( "bool result = (x < y);" ); |
38 | print_bool(result); |
39 | |
40 | /* demonstrate <= operator */ |
41 | result = (x <= y); |
42 | printf ( "bool result = (x <= y);" ); |
43 | print_bool(result); |
44 | |
45 | |
46 | /* keep console screen until a key stroke */ |
47 | char key; |
48 | scanf (&key); |
49 | } |
Here is the output:
x = 10
y = 20
bool result = (x == y);false
bool result = (x != y);true
bool result = (x > y);false
bool result = (x >= y);false
bool result = (x < y);true
bool result = (x <= y);true
0 comments:
Post a Comment