C Greater-Than Or Equal-To Operator
In C, the Greater-Than Or Equal-To >=
operator is a comparison operator used to check whether the left operand is greater than or equal to the right operand. If the condition is true, it returns 1
(true); otherwise, it returns 0
(false).
The Greater-Than Or Equal-To >=
operator is commonly used in conditional statements and loops to control program flow.
Syntax of the Greater-Than Or Equal-To Operator
The syntax to use the Greater-Than Or Equal-To operator is:
variable1 >= variable2
Explanation:
variable1
: The first operand to compare.>=
: The greater-than or equal-to comparison operator.variable2
: The second operand to compare with the first operand.
The result of the comparison is 1
(true) if variable1
is greater than or equal to variable2
, otherwise 0
(false).
Examples of the Greater-Than Or Equal-To Operator
1. Comparing Two Integers
In this example, we will compare two integer values using the Greater-Than Or Equal-To >=
operator and print the result.
main.c
#include <stdio.h>
int main() {
int a = 10, b = 5;
if (a >= b) {
printf("a is greater than or equal to b.\n");
} else {
printf("a is less than b.\n");
}
return 0;
}
Explanation:
- We declare two integer variables
a
andb
, initialized to 10 and 5. - The
if
condition checks ifa >= b
. - Since
10 >= 5
is true, the message “a is greater than or equal to b.” is printed.
Output:
a is greater than or equal to b.
2. Using Greater-Than Or Equal-To with User Input
In this example, we will take two numbers as input from the user and compare them using the Greater-Than Or Equal-To >=
operator.
main.c
#include <stdio.h>
int main() {
int num1, num2;
// Taking user input
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
// Checking condition
if (num1 >= num2) {
printf("%d is greater than or equal to %d.\n", num1, num2);
} else {
printf("%d is less than %d.\n", num1, num2);
}
return 0;
}
Explanation:
- We declare two integer variables
num1
andnum2
. - The user inputs values for
num1
andnum2
usingscanf()
. - The
if
condition checks ifnum1 >= num2
. - If true, it prints “
num1
is greater than or equal tonum2
“. Otherwise, it prints “num1
is less thannum2
“.
Output (Example):
Enter first number: 8
Enter second number: 6
8 is greater than or equal to 6.
3. Using Greater-Than Or Equal-To in a Loop
In this example, we will use the Greater-Than Or Equal-To >=
operator to control the termination of a loop.
main.c
#include <stdio.h>
int main() {
int i = 5;
// Loop runs while i is greater than or equal to 1
while (i >= 1) {
printf("i = %d\n", i);
i--;
}
return 0;
}
Explanation:
- We initialize an integer variable
i
to 5. - The
while
loop runs as long asi >= 1
. - Inside the loop,
i
is printed and then decremented by 1. - The loop stops when
i
becomes less than 1.
Output:
i = 5
i = 4
i = 3
i = 2
i = 1
Conclusion
The Greater-Than Or Equal-To >=
operator in C:
- Checks if one value is greater than or equal to another.
- Returns
1
if true, otherwise returns0
. - Used in conditions and loops to control program flow.