Write about Operators in ‘C' Language?
https://www.computersprofessor.com/2016/06/write-about-operators-in-c-language.html
C supports a rich set of built-in
operators.
Operator: operator is a symbol that tells the
computer to perform certain mathematical (or) logical manipulations.
Operators are used in programs to manipulate data & variables.
If
an operator acts on single operate then it is called unary operator. If an
operator acts on two operands then it is called binary operator. If an operator
acts on three operands then it is called ternary operator.
C
operators can be classified into number of categories. They include:
1. Arithmetic operators
2. Relational operators
3.Logical operators
4 .Assignement operators
5. Increment & Decrement
operators
6. Conditional operators
7. Bitwise operators
8. Special operator
|
||||||||||||||||||||||||||||||||||||
1. Arithmetic operators:
C
provides all the basic arithmetic operators arithmetic operators are used to
perform arithmetic calculations such as addition, subtraction, multiplication & division.
These can operate on any built in
datatype allowed in ‘C’.
|
||||||||||||||||||||||||||||||||||||
Integer arithmetic :
When
both the operands in a arithmetic exp are integers, the exp is called an
integer exp & the operation is called integer arithmetic. Integer
arithmetic always yields an integer value.
|
||||||||||||||||||||||||||||||||||||
Real arithmetic:
An arithmetic operation involving
only real operands is called real arithmetic.
Ex: x = 10.0/3.0 Þ 3.333
|
||||||||||||||||||||||||||||||||||||
Mixed mode arithmetic:
When one of the operands is real
& the other is integer, the expression is called a mixed mode arithmetic expression if either operand is of the real type, then only the real operation is
performed & the result is always a real number.
Ex: 15/10.0 Þ 1.5
|
||||||||||||||||||||||||||||||||||||
2. Relational operators:
Relational
operators are used to compare two quantities & depending on their relation,
take certain decisions. C supports 6 relational operators .
Ex: 10 < 20 is true ® 1
20 < 10 is false ® 0
The value of a relational expression is
either 1 or 0 It is 1 if the specified relation is true & 0 If the relation is false.
|
||||||||||||||||||||||||||||||||||||
3.Logical operators:
C has three logical operators
The
logical operators && and !! are used when we want to test more than
one condition and make decisions.
Ex: a > b && x == 10
Which
combines two or more relational expression is termed as a logical exp or a compound
relational expression.
Logical
expression also give a value of 1 or 0, according to the truth table. In the above
expression it give true only if a > b is true & x = = 10 is true. If either
of them are false, the expression Is false.
Ex:
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
4.Assignment operator:
Assignment operator are used to assign the result of an
expression to a variable.
assignment operator is =.
In addition, C has a set of shorthand assignment operators of
the form:
Syntax: V op = exp;
Where V is a variable exp is
an expression and op is a C binary arithmetic operator.
The operator ‘OP=’
Is known as the
shorthand assignment operator.These are the combination of both assignment and arithmetic operators. The above syntax is equal to
V = V OP (exp);
Ex: x + = 3
When this statemant is
executed, 3 is added to the old value of x. if x is 5 it becomes 8.
Ex: a = a % b a
% = b
a = a * (n + 1) a * = n + 1
a = a/(n + 1) a / = n + 1
|
||||||||||||||||||||||||||||||||||||
5. Increment &
decrement Operator:
C allows two very useful operators not generally found in
other languages. These are
Increment operator ++ and
Decrement operator --.
The operator ++ adds 1 to
the operand and while -- subtracts 1. Both are unary Operators.
Syntax: ++ m; (or) m ++ ;
++ m; is equal to m = m
+ 1, (or m+ = 1;)
– – m; (or) m – –;
– – m; is equal to m = m
– 1; (or m – = 1;)
We use the increment
& decrement statements in for & while loops extensively.
|
||||||||||||||||||||||||||||||||||||
when postfix ++(or – –)
is used with a variable in an expression, the expression is evaluated first using the
original value of the variable and then the variable is incremented (or
decremented) by one.
When prefix ++(or– –)
is used in an expression, the variable is incremented (or decremented) first &
then expression is evaluated using the new value of the variable.
Ex:– a = 5; a
= 5
pf(“%d”, a);–5 pf(“%d”, a);–5
pf(“%d”, ++a);–6 pf(“%d”, – – a);–4
pf(“%d”, a++);–6 pf(“%d”, a– –):–4
pf(“%d”, a);–7 pf(“%d”, a);–3
|
||||||||||||||||||||||||||||||||||||
6.Conditional operator:
Conditional operator use three operands to operate. That’s why it is
also called as ternary operator. It is denoted by ?:
Syntax:– exp1?exp2:exp3;
Where exp1, exp2&
exp3: are expressions.
The operator ?: works as follows:
exp1 is evaluated first.
If it is true, then the expression, exp2 is evaluated & becomes the value of the
expression. If exp1 is false, exp3 is evaluated and its value of the expression.
Note that only one of
the expression (exp2 or exp3) is evaluated.
Ex:– a = 10;
b =15;
x = (a>b)?a:b;
In this example, x will be assigned the value of b.
|
||||||||||||||||||||||||||||||||||||
7. Bitwise operator:
C has a distinction of
supporting special operators known as bitwise operators for manipulation of data at bit
level. These operators are used for testing the bits, or shifting then right or
left.
Bit wise operators may not be
applied to flood or double.
|
||||||||||||||||||||||||||||||||||||
8.Special operators:–
C supports some special operators such as comma
operator and sizeof operator.
Comma operator:
The comma operator can be used to link the
related expressions together.
Ex:– value = (x = 10, y = 5,
x+y);
First assigns the value
10 to x, then assigns the value 5 to y, and finally assigns 15 to value.
Exchanging values: t=x, x = y, y = t;
The sizeof operator:–
The sizeof operator is a compile time operator and when used with a operand, it returns the number of bytes the operand occupies.
The operand may be a variable, a constant or a datatype qualifier.
Ex:– m= sizeof (sum); –variable
n = sizeof (long int); –datatype.
k = sizeof (2352); –constant.
The sizeof operator is normally used to determine the
lengths of arrays & structures when their sizes are not known to the
programmer.
It is also used to allocated memory space dynamically
to variables during execution of a program.
|