Get Email Subscription

Enter your email address:

Delivered by FeedBurner

Sunday, December 27, 2009

Operators in 'C'

Operators in ‘C’

These are the special symbol in ‘C’ which is used to do specific process. There are six types of operator in ‘C’ as follows:-

1. Arithmetic Operator
2. Relational Operator
3. Logical Operator
4. Conditional Operator
5. Unary Operator
6. Assignment Operator

1) Arithmetic Operator:-
They are used to do different arithmetic process as follows:-

Operators Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% modulo division
(Gives Remainder after division)

There are three types of Arithmetic operator namely
i. Integer Arithmetic
ii. Real Arithmetic
iii. Mixed mode Arithmetic

i. Integer Arithmetic:-
It involves only the integer values / operands and gives the result only in the integer form.
For eg: - a = 15, b = 4
Then a + b = 15 + 4 = 19
a – b = 15 – 4 = 11
a * b = 15 * 4 = 60
a / b = 15 / 4 = 3
a % b = 15 % 4 = 3

ii. Real Arithmetic:-
It involves all the real value (floating points) and gives the result only in the real form
For eg:- a = 3.2, b = 0.3, c = -0.1
Then a + b = 3.2 + 0.3 = 3.5
a – b = 3.2 – 0.3 = 2.9
a * c = 3.2 * -0.1 = 0.32
a / b = 0.3 / 3.2 = 0.093
a % b = This has no values (Invalid)
Note that modulo division % is not done on real operands

iii. Mixed Mode Arithmetic:-
It involves some integer values and some real value but the result is always in the real format
For eg: - 2.5 + 3 = 5.5
3.271 – 2 = 1.271
2 – 3.271 = -1.271
4.1 * 8 = 32.8
25 / 10.0 = 2.5
25.0 / 10 = 2.5
3.3 % 2 = This has no values (Invalid)

Note that modulo division % is not done on real as well as mixed mode operands


2) Relational Operator:- These are the symbol which are used to show various relation. They are as follows:-

Operators Meaning
= = Equal to
! = Not equal to
<> Greater than
> = Greater than or equal to

In ‘C’ when the relation is false the value is 0 (zero) and whenever the relation is true the value is 1 (one)

For eg:-
Let a = 20, b = 10, c = 5.
Then,

Expression Interpretation Value
a > c True 1
a + b < a =" =" c =" =" a =" 10," b =" 20," c =" 4"> c && b True 1
b / c = = c | | a False 0
a – b > -15 && -20 True 1
a < c =" (a"> b) ? 10 : 20
Means if a > b is true then c = 10
if a> b is false then c = 20


5) Assignment operators:-
These are used to assign different value to the variables.
Expression:-

=, + =, - =, * =, / =, % =

Expression Meaning
a = 10 Assign value 10 to variable a
b = 3.9 Assign value 3.9 to variable b
a + = b a = a + b
a - = b a = a – b
a * = b a = a * b
a / = b a = a / b
a % = b a = a % b

Assignment operators is also called as shorthand operators


6) Unary Operators:-
These operators are used on a single variable operand to produce the result.
There are two types of unary operators namely Increment or Decrement operator

+ + is called an increment operator which means increase the value by 1
- - is called decrement operator which means decrease the value by 1

There are post increment / decrement and pre increment / decrement.

For eg:-
Let b = 10
Find a and b each of the following case


1) a = b + +
+ + appears after the variable b .; it is the post increment operator.
. ; value of b is assigned to a first
. ; a = 10
And then value of b is increased by 1
. ; b = 11


2) a = + + b
+ + appears before the variable b .; it is the pre increment operator.
In this case value of b increased by 1 firstly and then this value is assigned to a
. ; b = 11 and a = 11



3) a = b - -
- - appears after the variable b . ; it is the post decrement operator
. ; value of b is assigned to a first
. ; a = 10
And the value of b is decreased by 1
. ; b = 9


4) a = - - b
- - appears before the variable b .; it is the pre decrement operator.
In this case value of b decreased by 1 firstly and then this value is assigned to a
. ; b = 9 and a = 9

Related Posts by Categories



0 comments:

About This Blog

Lorem Ipsum

  © Blogger templates Newspaper III by Ourblogtemplates.com 2008

Back to TOP