Operators In C-Language- SeaOfComputer

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


Operators In C language



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 : 


Operands are the variables or constants on which the specified operator performs some operation. Each operator requires at least one operand to perform the operation. The following types of operators are classified on tie basis of required 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 : 

++   variable/constant     ,                      variable/constant ++
--     variable/constant     ,                      variable/constant -- 
!      variable/condition/constant

 

(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 :

variable  =  constant/variable 
variable  =  variable/constant + variable/constant 


(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- 


             [ condition expression ? statement 1: statement 2 ]


 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-


                              [ variablename = variable/constant; ]


For example : 


                                number     =  10; 
                                rate           =  6.50;
                                x               =  'A': 


(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)

The arithmetic operators are the binary operators requiring two operands for processing. The operator is placed in the middle of left and right operands. There is no operator for performing exponential operations in the C-language. 

The operators provided for arithmetic calculation can be used as follows - 

                          operand1 arithmetic operand2 operator ]

Most of the time, the arithmetic operators are used with the assignment operator, so that the arithmetic calculation result could be stored in a variable,

[ Variable = variable/constants arithmetic-operator variable/constant. ]



For example :

                             X=a+b; 
                             X=10*5; 

(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.
 

These operators are used in two manners as - prefix and postfix. If the operators (increment/decrement) are placed to the left side of operand, then it is called prefix, such as (operator operand) in following manner - 

++Variable/Constant 
--Variable/Constant 

If these operators are used to the right side of the operand, then this is called postfix method of using the operators, such as -

(operand operator) in following manner -

Variable/Constant++
Variable/Constant-- 

For example :
 
            Assume, the variable x has initial value 10, 
                        X =10; 

(i) - Output 'x' at this time is also 10. (Show x)  
(ii) - Output 'x++' also shows 10 (Show x++) [Postfix operation] 
(iii) - Output '++x' shows 11. (Show ++x ) [Prefix operation]


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


These operators perform the operations similar to simple arithmetic calculation as follows:

 (+=) 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:-


Post a Comment

Previous Post Next Post