-
Notifications
You must be signed in to change notification settings - Fork 0
section4_operators
Now that you know what a variable is and how to assign values, let's talk about operators.
An operator is just a symbol that operates on constant values and/or variables. Those operations can be arithmetic, logical, assignment (assign a value to some variable), etc.
int a = 1, b = 4, c = 0;
int sum = a + b + c;
In the example above, three variables are declared and initialized to some value. Notice that to assign a value to a variable, you are already using an operator, the =
. Then, the variable sum
is declared and an arithmetic expression is assigned, a+b+c
. Let's see what is actually happening, step by step.
- When the
=
operator is detected, what is on the right-side is seen as an expression that must be evaluated. Instead ofsum = a + b + c
, let's see it assum = expression1
. We don't know what expression1 is, we must evaluate it. - So, let's look inside expression1 and we get
a+b+c
. We detect a variable,a
, followed by an operator,+
. The operator+
, just like the assign operator (=), is binary, hence it expects two operands. So let's say we havea + expression2
. We don't know yet what that expression2 is, we need to evaluate it. -
expression2 is
b + c
. Another+
operator is detected after the variableb
, so what's on the right side needs to be evaluated. We now haveb + expression3
. - The expression3 is easy to evaluate because it's just the variable
c
, so we only need to load it's value.
Once we reach c
there are no further operators or expressions to be evaluated, so we start going backwards.
- Now that we know what expression3 is, we can evaluate
b + expression3 -> b + c -> 4 + 0
. Now let's step back again. -
expression2 is
b+c
which we evaluated above. So now we can evaluatea + expression2
. That's just1 + 4 = 5
. We can go backwards again. - The first expression we found, after the assignment operator, was expression1. That expression was
a+b+c
ora + expression_2
, that we just evaluated above to the value 5. So, finally we can assign the value ofa+b+c
to the variablesum
. Remember that the assignment operator,=
, well.. it's an operator. So, is the expressionsum = a+b+c
evaluated to some value? YES! It's evaluated to the same value you assign to the variablesum
. So, you are allowed to do stuff like this:
#include "stdio.h"
int main()
{
int a = 1, b = 4, c = 0, sum1, sum2;
sum2 = (sum1 = a+b+c); // sum2 is initialized to the same value as sum1
printf("Sum 2 = %d\nSum 1 = %d", sum2, sum1);
}
binary operators, what a terrific word! There's not much to say about this, just be aware that unary operators expect a single operand. Binary operators expect two operands, as we saw with the assignment operator =
and addition +
operator. This is common terminalogy on several languages.
These operators are used to perform basic math operations.
-
+
- Addition (binary) : sums two numbers.
- Unary plus (unary) : It performs a conversion. If you are curious, see this thread. https://stackoverflow.com/a/3903114
-
-
- Subtraction (binary) : subtracts two numbers.
- Unary minus (Unary) : changes the signal of the operand.
int a = 10; b = -a; c = -(10+a); // a=10, b=-10, c=-20
-
*
: Multiplication -
/
: Division -
%
: Reminder after division
Note: The result of an arithmetic expression depends on the operands data types. For example, if you add two integer numbers, the result is an integer number. If you sum two operands of mixed data types, like a integer and a double, the compiler will cast the operands till they all have the same data type. In this case, it doesn't make sense to convert the double operand to integer, that would lead to lost of the decimal part. Instead, it will convert the integer to double and then add the operands.
#include "stdio.h"
int main() {
float a = 5/2; // 2.0000
float b = 5.0/2; // 2.5000
float c = (float) 5/2; // 2.5000
float d = (float) (5/2); // 2
int e = 5.0/2; // 2.0000
}
As you see, on the first line we divided two integer numbers and the result is a integer, despite the fact the variable a
is float
. For that reason, the result is 2.0 instead of 2.5.
For the variable b
we set one of the operands as double (writing 5.0 is interpreted as double data type). The other operand is a integer, but the compiler is smart enough and will convert it to double before the operation. Of course, you could also use 5/2.0
.
Another way to achieve the correct result is to use type casts, at least on one operand. On the example, (float) 5
, the integer 5 is converted to float, which forces the second operand of the division to be converted to float as well. A simlar example is shown for variable d
. However, a precedance is introduced and the typecast is only applied after integer division of 5 by 2.
The last example represents a case where a floating number is assigned to a integer type variable. The decimal part is lost because is truncated (it's not rounded).
Relational operators check the relation between two operands. These operators introduce the notion of true and false, known as boolean in other languages. C doesn't actually offers a data type to for true/false notion (until C99). Instead, it uses numbers. In relations, a 0
means false and any other value means true. This is important to keep in mind!
However, an expression with one relational operator will either evaluate to zero (false) or one (true).
Operator | Meaning | Example |
---|---|---|
== | Equal to | 5=5 evaluates to 1 |
> | Greater than | 5 > 55 evaluates to 0 |
< | Less than | 0 < 1 evaluates to 1 |
!= | Not equal to | 0 != 1 evaluates to 1 |
>= | Greater than or equal to | 5 >= 5 evaluates to 1 |
<= | Less than or equal to | 3 <= 6 evaluates to 1 |
Logical operators are very important to make decisions, as you will see later. Like Relational Operators, the logical operators also evaluate to 0 (false) or 1 (true).
Operator | Meaning | Example |
---|---|---|
&& | AND. Returns true if all operands are true. |
(10 > 1) && (0 == 0) evaluates to 1 |
|| | OR. Returns true if at least one operand is true. |
10 > 1 || 0 == 1 evaluates to 1 because 10 > 1 is true. |
! | NOT. True if the operand is false (0), otherwise returns false. |
!0 evaluates to 1 |
There are other operators, that you can see here https://www.tutorialspoint.com/cprogramming/c_operators.htm
Soon...