January 20, 2013

Operators in C and C++

operators in C and C++ programming


Operators are symbols which are used to perform specific operations it means every operator has a predefined task which is assigned to it.

Some of them are:
1. Arithmetic Operator
2. Relational Operator
3. Logical Operator
4. Assignment Operator
5. Urinary Operator
6. Conditional Operator
7. Bitwise Operator
8. Shift Operator
9. Special Operator 


ARITHMETIC OPERATOR

arithmetic operators in C and c++ programming language


These are the operators which can perform arithmetic operations like addition, subtraction, multiplication, division and remainder. 

When both operands in an expression are integers then the operation is called integer arithmetic and its answer is always an  integer value.
An arithmetic operation involving real operands is called Real Arithmetic. In such a case answer will be real.

When one operand is real and other is integer the expression is called Mixed Mode Arithmetic Operation and result of such expression is real number.


RELATIONAL OPERATOR

These operators are used to compare variables, constants, variable and constants, variable with expression and so on …


relational operators used in C and C++ programming language



LOGICAL OPERATOR

These operators are those operators in which two relational expression are connected and on the basis of their values these will give answer in true (T) or false (F).


logical operators used in C and C++ programming language



ASSIGNMENT OPERATOR

These operators are those which assign either a constant to a variable, or variable to variable, or an expression to a variable.
It means to the left hand side of assignment symbol there is a single variable and to the right hand side there may be a variable, a constant or any other expression.


assignment operator used in C and C++ programming language



URINARY OPERATOR

These operators are called increment or decrement operators. These are commonly expressed as ‘++’ or ‘--’
++ : operator is used for a unit increment
 --  : operator is used for a unit decrement

These operators can be used in two forms i.e. Prefix and Postfix form.

In the Prefix form operator is used before the variable
If a=5
++a or --a
Let b= ++a i.e. a=6, b=6;

In the  Postfix form operator is used after the variable
If a=5
a++ or a--
Let b= a++ i.e. a=6, b=5;

When operator is used in prefix form the value of variable is first incremented and there after it is assigned to the variable.

In postfix form operator is used after the operand. In this form first value of variable is assigned to the variable on the left hand side of the expression and there after it is incremented or decremented by one.



CONDITIONAL OPERATOR

This operator is an alternative form of “if else” statement. It means this operator uses three statements in its syntax in which 

First is an expression
Second is a statement
Third is also a statement.

If the first expression is true then adjoining statement will be executed but if an expression if false then the third statement will be executed.

Format is as:
(expression)? statement1: statement2;

Like:
If a=50
(a>60)? a=a-5: a=a+3;
Since according to our condition the expression is false so second statement is executed i.e. a=50+3;

If a=70
Since now expression holds true so first statement is executed i.e. a=70-5;



No comments:

Post a Comment