In this tutorial, I’ll decribed most of the commanly used operators in C. There are 2 types of operators: logical and arithematic. Logical operations produce a value of true or false. Arithematic operations return a calculated value.
The logical operators are:
> - greater than – x > y means compare x to y and if x is greater than y then return true otherwise return false
>= – greater than or equal to – works the same as > except if the values are equal then it also returns true
< – less than – works the same as > except if x is less than y then it returns true
<= – less than or equal to – works the same as < except if the values are equal then it also returns true
== – equal to – returns true if the values are the same otherwise returns false
!= – not equal to – opposite of equal to
! – not – changes false to true and true to false – eg. !(x > y) would be the same as <= y. This does have uses, I promise
&& – and – requires that both the value on its left and the value on its right be true to return a true – eg. (x > y) && (x > z) means x must be greater than both y and z for this to evaluate to true
|| – or (2 pipe symbols) – requires that either the value on its left or the value on its right be true to return a true – eg. (x > y) || (x > z) means x must be greater than either y or z for this to evaluate to true
In the previous post, I used some arithematic operators. Here is a longer list:
= – assignment operator – performs all the calculations to the right of it to produce a single value and then assigns that value to the variable to the left of it. eg. x = y + z adds y and z and stores the result in x. The existing value that was in x is replaced.+ – add
- – subtract
* – multiply
/ – divide
% – modulus – this is an integer calculation. It produces the remainder from integer division. eg. 3 % 3 is 0, 4 % 3 is 1, and 5 % 3 is 2+= – add assign – x += y adds x and y and stores the results in x. The remainder of the assigns work the same way
-= – subtract assign
*= – multiply assign
/= – divide assign
%= – modulus assign++ – increment – can be used in 2 ways: preincrement ++x or postincrement x++. Increment increases the value stored in a variable by 1. The difference between pre and post increment is whether the increment is performed before or after the variable is used in the current line of code.
So, x = y + ++z increases z by 1 and then adds y and z and stores the result in x.
x = y + z++ adds y and z and stores the result in x and then adds increases z by 1.– – decrement – works just like increment except that it decreases the value in the variable by 1.
Thats not all the operators, but we can do a lot of things with these. I’ll introduce other operators when I use them in a sample.
Recent Comments