Operators
An expression is a sequence of operators and their operands, that specifies a computation.
Binary Operators
+
Add two values
-
Subtract one value from another
*
Multiply two values
//
Divide two values
%
Modulo of a value
<
Check if one value is less than another
<=
Check if one value is less than or equal to another
>
Check if one value is greater than another
>=
Check if one value is greater than or equal to another
!=
Check if one value is different from another
==
Check if two values are equal
Here is an example of each binary operators:
+
(a = (1 + 2))
Here a = 3.
-
(a = (2 - 1))
Here a = 1.
*
(a = (2 * 2))
Here a = 4.
//
(a = (10 // 2))
Here a = 5.
%
(a = (15 % 10))
Here a = 5.
<
(if (1 < 2) {
(a = 10)
})
Here a = 10.
<=
(if (2 <= 2) {
(a = 10)
})
Here a = 10.
>
(if (2 > 1) {
(a = 10)
})
Here a = 10.
>=
(if (2 >= 2) {
(a = 10)
})
Here a = 10.
!=
(if (2 != 5) {
(a = 10)
})
Here a = 10.
==
(if (5 == 5) {
(a = 10)
})
Here a = 10.
Operators Priority
The C-- defines different priority for each operators, it is defined by a number that go from 1 (very low priority) to 9 (biggest priority)
Here is a table with the default priority for operators:
4
[ +, -]
5
[ *, //, % ]
6
[==, !=, >, >=, <, <=]
Considering the following example, we can see that we can chain operators when we want to compute values:
(int i = (1 + 6 // (3 * 2) * 2))
(int b = (1 + 6 // 3 * 2 * 2))
Here, the integer i
will be equal to 3 and b
equal to 9 considering the calculation priority.
Infix Operators
In C-- it's possible to change the operator priority and it's possible to assign an operating priority to functions
Example:
(func (tona (int a) (int b))
{
(a)
})
(infixl * 2)
(infixl tona 3)
(int i = (2 + 1 tona 7 * 3))
In this case tona
is considered as an infix operator and the *
operator has a lower priority than tona
. So here i = 3 tona 7 * 3
. So i = 3 * 3 = 9
.
Another example:
(func (tona (int a) (int b))
{
(a + b)
})
(infixl tona 3)
(int a = 1)
(a `+`= 1)
(a `tona`= 4)
In this case tona
is considered as an infix operator but it acts like the +
operator. In C-- `+`=
means +=
. So here a = 6
.
Unary Operators
&
Access the address of a value
?
Dereference a pointer
++
Increment
--
Decrement
Here is an example of each unary operators:
&
(func (putchar (char c))
{
(write 1 (&c) 1)
})
Here the character stored in the variable c
is written.
?
((func (ups (int * h)) {
((h[0]) = ((?h) + 1));
}))
Here (?h)
dereference its pointer.
++
(string strg = "Hello")
(int a = 1)
(++a)
(if (a == 2) {
puts(strg)
})
Here the string strg
is displayed because the value a
is incremented to 2
with the ++
operator.
--
(string strg = "Hello")
(int a = 1)
(--a)
(if (a == 0) {
puts(strg)
})
Here the string strg
is displayed because the value a
is incremented to 2
with the --
operator.
Post-fix Operators
[]
Access an index
[]
(int i = 5)
(char * str)
((str[i]) = 'c')
Here the 5th character of str
is c.
Last updated