Content In This Blog
- Unary Operators
- Binary Operators
- Ternary Operators
- Assignment Operators
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Increment and Decrement Operator
- Compound Assignment Operators
Operators In a C-Program
Operator is a symbol associated with an action or operation on data such as arithmetic calculation, assignment or logical operations. C-language has a rich set of operators, which can be categorized as two types : based on number of operands and based on their operations.
Operators Based on Number of Operands :
(a) Unary Operators
The unary operators require only one operand to perform the specific operation. The operator written to the left or right side of the operand. C-language provides many operators to be used as unary like increment operator (++), decrement operator (--), not operator (!). These operators are used differently for different purpose.
For example :
(b) Binary Operators
The binary operators work on two operands. The operator is placed in between the left and right operands. The operands may be the variable or constant.
There are many binary operators provided by C-language which we can use for most of the arithmetic and logical operations.
These are =, +, - . 1, %, <, >, && :: and many others like <=, >=, !=.
These operations are used for several purposes.
These are to be used in a programming instruction as following -
operand1 operator operand2
For example :
(c) Ternary Operators
Ternary operators operate on three operands. The conditional operators (? :) are the ternary operators provided by C-language.
The ternary operators provided by C-language works for decision making as follows-
Operators Based on the Operations:
The operators operate on the data value and produce the results based on the operations. The operations performed by the operators are of many types such as arithmetic operations, logical operations etc. Following are the operators based on the operation to be performed on operands-
(a) Assignment Operator
The 'equals to' (=) operator works as the assignment operator in C-language. This operator is used to assign/store a data value in a variable. There should be used the variable name on the left side of the assignment operator.
But the right side of assignment operator may be any variable or any constant. operator stores the data value of the right side operand to the left side operand. This operator is used in following manner in a C-program-
For example :
(b) Arithmetic operators
The arithmetic operators are used to perform the arithmetic calculations on data value either variable or constant C-language provides following operators for performing arithmetic calculations :--
Operator | Operation |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus (Reminder) |
For example :
(e) Comparison Operators
In a C-program, we may require some comparison of data values for decision making process. These are also known as Relational operators. For this work C-language provides many comparison operators described below with their functions:
Operator | Operates on |
---|---|
= = | Equals to |
< | Lesser than |
> | Greater than |
<= | Lesser than or equals to |
>= | Greater than or equals to |
! | Not equals to |
These comparison operators require two operands for performing operations. These operators return a Boolean value either True or False after the evaluation of comparison of two data values.
(d) Logical Operators
These are called "Boolean operators", because the comparison between values or operands is reduced to either "True' or 'False'. The values returned by logical operators are zero or one.
Zero indicates to False value and One to True. These operators never change the value of operands.
Following logical operators are provided by C-language -
Operator | Action |
---|---|
&& (AND) | Evaluate first and than second and so on and works for all |
|| (OR) | Evaluates both all the operands and works for any one. |
! (NOT) | Evaluates of negation of the operand. |
(e) Increment and Decrement Operator
These both are unary operators. These operators specify that the operand is either increased or decreased by one only.
Operator | Usage |
---|---|
++ (Increment operator) | Increment by one |
-- (Decrement operator) | Decrement by one. |
In this example, (ii) step shows that the increment in the value of x is performed after display because, it is postfix operation. Thus, in postfix operation the increment or decrement is performed after assignment.
The step (iii), states that the increment is performed prior to other operation, because the operator is used as prefix.
(f) Compound Assignment Operators
These operators are the combination of arithmetic operator and the assignment operator. These operators first perform the arithmetic operation and then assignment operation.
Operator | Usage |
---|---|
+= (Add Assignment) | operand1 += operand2 |
-= (Substract Assignment) | operand1 -= operand2 |
*= (Times Assignment) | operand1 *= operand2 |
/= (Divide Assignment) | operand1 /= operand2 |
%= (Modulus Assignment) | operand1 %= operand2 |
(+=) operates like ( varl+=var2). It is similar to (varl=varl+var2).
For example :
x=10;
y=20;
x=x+y; or x+y;
Both results in 30 as the value of x.
Releted Posts:-