Comparison operators
Equality operator
In the previous section, we introduced the == operator, and operators of this type are called equality operators.
There are two types of equality operators available.
These operators are used to compare whether values are equal or not.
The following program checks if the input is 10.
If you run this program and enter 10 as input, the result will be as follows:
If you run this program and enter a number other than 10, the result will be as follows:
There are two types of equality operators available.
| symbol | Become true | Become false |
|---|---|---|
| == | The two values are equal. | The two values are not equal. |
| != | The two values are not equal. | The two values are equal. |
These operators are used to compare whether values are equal or not.
The following program checks if the input is 10.
Source code
#include <stdio.h>
int main(void)
{
int suuti;
scanf("%d", &suuti);
if (suuti == 10)
printf("The input value is 10.\n");
if (suuti != 10)
printf("The input value is not 10.\n");
return 0;
}
If you run this program and enter 10 as input, the result will be as follows:
Results
10 Input Data
The input value is 10.
The input value is 10.
If you run this program and enter a number other than 10, the result will be as follows:
Results
135 Input Data
The input value is not 10.
The input value is not 10.
As it is an operator.
These comparison operators, along with the relational and logical operators explained next, are also operators. In other words, they can be placed within expressions and used for calculations.
The following expression assigns 1 to `answer` (in most compilers) if the variables `val1` and `val2` are equal, and 0 if they are not equal. `answer = val1 == val2;`
While some advanced programmers prefer this approach, it is not particularly clear and therefore not recommended.
The following expression assigns 1 to `answer` (in most compilers) if the variables `val1` and `val2` are equal, and 0 if they are not equal. `answer = val1 == val2;`
While some advanced programmers prefer this approach, it is not particularly clear and therefore not recommended.
Relational operators
The equality operator was an operator used to determine if two values were equal.
Relational operators are used to compare the relative size of two values.
It may seem obvious, but the difference with being small is...
It's a difference of whether or not it includes equalities.Less than or equal to and greater than or equal to also return true when they are equal.
These operators are used to compare the relative size of two values.
The following program checks if the input number is greater than 10.
If you run this program and enter 10 as input, the result will be as follows:
The result when running this program with an input greater than 10 is as follows:
If you run this program with an input less than 10, the result will be as follows:
Relational operators are used to compare the relative size of two values.
| symbol | Become true | Become false |
|---|---|---|
| < | The left value is smaller than the right. | The left value is not smaller than the right |
| > | The value on the left is greater than the value on the right. | The left value is not greater than the right |
| <= | The left value is less than or equal to the right value | The left value is not less than or equal to the right value. |
| >= | The left value is greater than or equal to the right value. | The left value is not greater than or equal to the right value. |
It may seem obvious, but the difference with being small is...
It's a difference of whether or not it includes equalities.Less than or equal to and greater than or equal to also return true when they are equal.
These operators are used to compare the relative size of two values.
The following program checks if the input number is greater than 10.
Source code
#include <stdio.h>
int main(void)
{
int suuti;
scanf("%d", &suuti);
if (suuti == 10)
printf("The input value is 10.\n");
if (suuti > 10)
printf("The input value is greater than 10.\n");
if (suuti < 10)
printf("The input value is less than 10.\n");
return 0;
}
If you run this program and enter 10 as input, the result will be as follows:
Results
10 Input Data
The input value is 10.
The input value is 10.
The result when running this program with an input greater than 10 is as follows:
Results
135 Input Data
The input value is greater than 10.
The input value is greater than 10.
If you run this program with an input less than 10, the result will be as follows:
Results
5 Input Data
The input value is less than 10.
The input value is less than 10.
Logical operators
Logical operators are operators with a slightly different nature than the operators we've seen so far.
It can be used to combine multiple conditions or to invert a judgment.
In if statements, you can only evaluate one condition at a time.
By using these operators, you can make judgments based on multiple conditions.
The following program checks if an input number is between 8 and 12.
If you run this program and enter a number between 8 and 12, the result will be as follows:
If you run this program and enter a number outside the range of 8-12, the result will be as follows:
The key to this program lies in the condition part of the if statement.
Since an if statement can only evaluate one condition, it's not possible to determine whether a value is 8 or greater and 12 or less using a typical approach.
However, the && operator allows for evaluation based on two conditions.
The second if statement's condition is simply the first condition with a negation operator added.
The ! operator is an operator that inverts the result of a judgment.
It will produce the exact opposite result from the first if statement.
It can be used to combine multiple conditions or to invert a judgment.
| symbol | Meaning | Become true | Become false |
|---|---|---|---|
| && | and (AND) | Both the right and left conditions are true | If either the right or left condition is false |
| || | or (OR) | Either the right or left condition is true | Both the right and left conditions are false |
| ! | not (NOT) | The condition is false | The condition is true |
In if statements, you can only evaluate one condition at a time.
By using these operators, you can make judgments based on multiple conditions.
The following program checks if an input number is between 8 and 12.
Source code
#include <stdio.h>
int main(void)
{
int suuti;
scanf("%d", &suuti);
if (suuti >= 8 && suuti <= 12)
printf("Between 8 and 12.\n");
if (!(suuti >= 8 && suuti <= 12))
printf("It is not between 8 and 12.\n");
return 0;
}
If you run this program and enter a number between 8 and 12, the result will be as follows:
Results
9 Input Data
Between 8 and 12.
Between 8 and 12.
If you run this program and enter a number outside the range of 8-12, the result will be as follows:
Results
3 Input Data
It is not between 8 and 12.
It is not between 8 and 12.
The key to this program lies in the condition part of the if statement.
Since an if statement can only evaluate one condition, it's not possible to determine whether a value is 8 or greater and 12 or less using a typical approach.
However, the && operator allows for evaluation based on two conditions.
The second if statement's condition is simply the first condition with a negation operator added.
The ! operator is an operator that inverts the result of a judgment.
It will produce the exact opposite result from the first if statement.
About This Site
Learning C language through suffering (Kushi C) isThis is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.




