Usages and Definitions of All Operators in C
There is a list of all operators in C.
- Assignment Operator
- Arithmetic Operators
- Cast Operator
- Increment and Decrement Operators
- Abbreviated Assignment Operators
- Relational Operators
- Logical Operators
I. Assignment Operator
Equal sign (=) is used as assignment operator in C and it must be read as “is assigned the value of”.
That means: a is assigned value of b. There is also this usage that allows you to make multiple assignments like this:
All of the variables are equalized to 153. The precedence of assignment operator is right to left. That means first o is equalized to 153, then c is assigned value of o which is 153, finally r is assigned value of c.
Example 1.0:
Output of Example 1.0:
II. Arithmetic Operators
|
Operator |
Description |
|
+ |
Addition |
|
- |
Subtraction |
|
* |
Multiplication |
|
/ |
Division |
|
% |
Remainder ( or Modulus ) |
Example 2.0:
Output of Example 2.0:
III. Cast Operator
Sometimes results of arithmetic operations can be unexpected. For example; division of two integer number may return a float number, but there will be an information loss because we cannot get a float number from integers. So, what should we do?
As you see from the title, we should use the Cast operator. The Cast operator allows you to change the type of number temporarily before the next computation.
Example 3.0:
Output of Example 3.0:
As you see, we changed the type of result of y/x in line 10 to float via (float) operator. You can define the (type) operator as you need.
IV. Increment and Decrement Operators
|
Operator |
Description |
|
++ |
Increment Operator |
|
-- |
Decrement Operator |
These operators are used to make operations easier and faster. Increment operator adds 1; decrement operator subtracts 1 from the number.
4.1 ) Preincrementation and Predecrementation
|
Usage |
Equals To |
|
x=++a -b; |
a=a+1; |
|
y=--c -d; |
c=c-1; |
4.2 ) Postincrementation and Postdecrementation
|
Usage |
Equals To |
|
x=a++ -b; |
x=a-b; |
|
y=c-- -d; |
y=c-d; |
Example 4.0: All usages of increment and decrement operators
Output of Example 4.0:
V. Abbreviated Assignment Operators
|
Operator |
Description |
|
+= |
Assign by Sum |
|
-= |
Assign by Subraction |
|
*= |
Assign by Product |
|
/= |
Assign by Quotient |
|
%= |
Assign by Remainder |
Usages of Abbreviated Assignment Operators:
|
Usage |
Equals To |
|
x+=5; |
x=x+5; |
|
x+=d; |
x=x+d; |
|
y-=7; |
y=y-7; |
|
r = c -= o + g; |
r=(c=(c-(o+g))); |
|
r = c -= o + g; |
c=(c-(o+g)); |
Example 5.0: Proving equality of expressions
Output of Example 5.0:
VI. Relational Operators
|
Operator |
Description |
|
< |
is less than |
|
> |
is greater than |
|
<= |
is less than or equal to |
|
>= |
is greater than or equal to |
|
!= |
is NOT equal to |
|
== |
is equal to |
These are can be used as conditional operators in C. If the condition is true then it returns 1, if condition is false then it return zero.
Example 6.0:
Output of Example 6.0:
VII. Logical Operators
|
Operator |
Description |
|
&& |
Logical AND |
|
|| |
Logical OR |
|
! |
Logical Negation ( NOT ) |
To compare exist conditions and to generate new conditions we use this logical operators. The associativity from highest to the lowest is ! (NOT), && (AND), || (OR), but you can use parenthesis to change the hierarchy.
The relational operators have higher precedence than logical operators.
Example 7.0:
Output of Example 7.0:
Precedence of Operators
|
Precedence |
Operator |
Associativity |
|
1 |
( ) |
Innermost First |
|
2 |
+ |
Right to Left (Unary) |
|
3 |
* |
Left to Right |
|
4 |
+ |
Left to Right |
|
5 |
< |
Left to Right |
|
6 |
== |
Left to Right |
|
7 |
&& |
Left to Right |
|
8 |
|| |
Left to Right |
|
9 |
( ) |
Right to Left |

Recently Typed