C Language Examples - Comparing Numbers
Compare two numbers
The following example defines two integer variables and uses if to compare the two values; you can first look at the logic diagram:

Example
#include
<
stdio.h
int
main
(
)
{
int
a
,
b;
a
=
11
;
b
=
99
;
// You can also use the following code to let the user enter two numbers in the terminal
// printf("Enter the first value:");
// scanf("%d", &a);
// printf("Enter the second value:");
// scanf("%d", &b);
if
(
a
b
)
printf
(
"
a is greater than b
"
)
;
else
printf
(
"
a is less than or equal to b
"
)
;
return
0
;
}
Output result:
a 小于等于 b
Compare three numbers
The following example defines two integer variables and uses if to compare three values; you can first look at the logic diagram:

Example
#include
<
stdio.h
int
main
(
)
{
int
a
,
b
,
c;
a
=
11
;
b
=
22
;
c
=
33
;
if
(
a
b
&&
a
c
)
printf
(
"
%d is the largest
"
,
a
)
;
else
if
(
b
a
&&
b
c
)
printf
(
"
%d is the largest
"
,
b
)
;
else
if
(
c
a
&&
c
b
)
printf
(
"
%d is the largest
"
,
c
)
;
else
printf
(
"
Two or three values are equal
"
)
;
return
0
;
}
Output result:
33 最大
C Language Examples