Operators in C
-
Arithmetic operators
-
Cast operator
-
Increment and Decrement
-
Bitwise operators
-
Comparison operators
-
Assignment operators
-
sizeof operator
-
Conditional expression operator
Arithmetic Operators
C supports the arithmetic operators :
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo (remainder)
The Cast Operator
The cast operator temporarily changes the type of a variable
if either operand is a double,
Increment and Decrement Operator
c has two special operators for adding and subtracting one from a variable
++ iincrement
-- decrement
These may be either prefix (before the variable) or postfix (after the variable):
Example :
int i =5, j = 4;
i++; /*“i’ becomes 6 */
j++; /*“j”becomes3 */
--i; /*”1” becomes 7 */
Prefix and Postfix
- The prefix and postfix versions are different
here first is equivalent to :
- j++;
- i=j;
second is equivalent to :
- i=j;
- j++;
Result is like
i=6 and j=6;
i=5 and j=6;
Comparison Operators
- C supports the comparison operators:
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== is equal to
!= is not equal to - These all give 1 (non zero value, i.e. true) when the comparison succeeds and 0 (i.e. false) when the comparison fails
Logical Operators
- C supports the logical operators:
&& and
|| or
! not
- These also give I (non zero value, i.e. true) when the condition succeeds and 0 (i.e. false) when the condition fails
Bitwise Operators
C has the following bit operators which may only be applied to integer types:
& bitwise and
| bitwise inclusive or
^ bitwise exclusive or
~ one’s compliment
>> right shift
<< left shift
Assignment
Assignment is more flexible than might first appear
An assigned value is always made available for subsequent use
“n= 22” happens first, this makes 22 available for assignment to “m”. Assigning 22 to “m” makes 22 available for assignment to “I” etc.
“j” is assigned 93, the 93 is then made available to printf for printing.
Other Assignment Operators
There is a family of assignment operators:
+= -= *= /= %=
&= |= ^=
<<= >>=
In each of these:
expression1 op= expression2
is equivalent to:
(expression1) = (expression1) op (expression2 )