Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views8 pages

C Operators - Refrence Reema Thereja Book

Operators of C reference from Reema Thereja Book
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

C Operators - Refrence Reema Thereja Book

Operators of C reference from Reema Thereja Book
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Operators:

As operator is a symbol that specifies the mathematical,logical, orrelational operation tobe


performed. The operators are used withvariables and constants to form expressions.
The major groups to which operators belong to are:
– Arithmetic operators - Relational operators - Equality operators
– Logical operators - Unary operators - Conditional operators
– Bitwise operators - Assignment operators - Comma operator
– sizeof operator
Arithmetic Operators:
Let variable A hold 8 and B hold 3. They are of type int.
Operator Syntax (int A=8, B=3) Result Use
+ A+B 11 Addition
- A-B 5 Subtraction
* A*B 24 Multiplication
/ A/B 2 Division
% A%4 0 Modulus: gives
remainder
Note:
 All operators except modulus (%) take mixed integer and float numbers.
 If both operands are integer then result is integer number.
 If both or one of operands is float, then result will be floating point number.
 All the arithmetic operators bind from left to right.
 Multiplication, division and modulus have higher precedence over the addition and
subtraction operators.Ex: if an arithmetic expression consists of a mix of operators, then
multiplication, division, and modulus will be carried out first in a left to right order,
before any addition and subtraction could be performed.Ex: 3 + 4 * 7= 3 + 28 //
4 * 7 is calculated before 3 + 4= 31
Division operator (/):
 When both operands of the division operator (/) are integers, the division is performed as
an integer division and always results is integer. Ex: 8 / 4 = 2
 When both operands are integer, the result or quotient may not be integer, i.e., 1 / 2 = 0.5
 Two cases are there:
 If op1 and op2 have the same sign, then op1/op2 is thelargest integer less than
the true quotient.
o a= 9, b= 4. Both are integer and positive. a/b = 9/4= 2
o actual value of 9/4 is 2.25 which islargest integer less than the true
quotient.
 If op1 and op2 have opposite signs, then op1/op2 is the smallest integer greater
than true quotient.
o a= - 9, b=4. a is negative and b is positive. a/b = - 9/ 4 = -3
o actual value of 9/4 is -2.25.But output of integer division is -3 as it
issmallest integer greater than true quotient. – 3 is smaller than -2.25.
 Dividing any number by zerois an illegal operation that results in a run-time division-
by-zero exception, thereby terminating the program.
Modulus operator %:
 Finds the remainder of an integer division. This operator can be applied only to integer
operands and cannot be used on float or double operand.
Ex: float a=10.2;
printf(“Remainder is = %f”, a%2); // generates error as a is float
 While performing modulo division, the sign of the result is always the sign of the first
operand (the dividend).
Ex: -16 % 3 = -1 16 % 3 = 1 16 % -3 = 1
Relational Operators:
Relational operator is also known as a comparison operator,is an operator that compares two
values. Expressionsthat contain relational operators are called relationalexpressions. Relational
operators return true or false value, depending on whether the conditional relationshipbetween
the two operands holds or not.The relational operators are evaluated from left to night.
Ex: To test if x is less than y, relational operator < is used as x < y. This expression will return
true(1) ifx is less than y; otherwise the value of the expression willbe false(0). Relational
operators can be used to determine therelationships between the operands.Assume x=8, y= 3,
then x<y i.e., 8<3 is false, so returns false (0).
Relationships are:

 Characters are considered valid operands since they are represented by numeric values
in the computer system. The operands of a relational operator must evaluate to a
number.
Ex: ‘A’ < ‘B’, where A is 65 and B is 66 then the result would be 1 as 65 < 66 (ASCII
Code).
 Arithmetic expressions can be used. then first the arithmetic expression will be
evaluated and then the result will be compared This is because arithmetic operators have
a higher priority over relational operators.
Note: The expressions are true if their value is not zero, if their value is zero, then
evaluates to false.
Ex: If x=1, y=2, and z = 3, then
(x) evaluates to true, as x value is 1
(z*9) evaluates to true, as z*9= 27
(x-1) evaluates to false, as x value is x-1=1-1=0
(0 * y) evaluates to false as 0 * 2 is 0
(z- x+y) evaluates to true as expression evaluates to non-zero (3-1+2) is 4
(y == 1) evaluates to false as y is not equal to 1
(y % 2) evaluates to false as expression evaluates to 0
Note:
 Blank spaces are allowed between an operand and an operator
 No space is permitted between the components of an operator (like >= is not allowed, it
should be >=). Therefore, writing x==y is correct but writing x = = y is not acceptable
in C language.
Equality Operator: Two kinds of equality operators to compare their operands for strict
– Equality equal to (= =) operator
– Inequality not equal to (!=) operator.
• The equality operators have lower precedence than the relational operators.

Logical Operator:
It is used to combine more than one Condition. LogicalAND (&&), logical OR (||), and logical
NOT (!). Ex: to combined twoconditions A<B and B>C, then && is used (A<B) && (B>C).
Logical AND
– Logical AND operator is a binary operator,
– It simultaneously evaluates two values or relational expressions.
– If both the operands are true, then the whole expression evaluates to true. If both
or one of the operands is false, then the whole expression evaluates to false.
– Truth table of Logical AND (&&)

The expression to the left is (a < b) and that on theright is (b > c). The whole expression
is true only ifboth expressions are true, i.e., if b is greater than bothaand c.
Ex: (a <b) && (b > c)
For input a =9, b=3, c=1 its True because both 9>3 and 9>1 are true.
For input a =9, b=3, c=10 its False because 9 > 3 is true but 3>10 is false.
 Logical OR (||)
– Logical OR returns a false value if both the operands are false, otherwise it returns a
true.
– Truth Table

– If at-least one operand is true, it results in true.


– Ex: (a <b) || (b> c)
The expression to the left is (a < b) and that on theright is (b > c). The whole
expression is true if either b isgreater than a or b is greater than c or b is greater
thanboth a and c.
Ex: For input a =9, b=3, c=1 its True because 9<3 is false but 3>1 is true.
For input a =9, b=3, c=10 its False because both 9 < 3 and 3>10 are false.
 Logical NOT (!)
– It takes a single expression and negates or reverses the value of expression.
– Logical NOTproduces a 0 if the expression evaluates to a non-zero valueand
produces 1 ifthe expression produces a zero.

– if x=5 and !x is false(0) because x has 5, a non-zero number is true, reverse of true
is false
Unary Operators:
Unary operators act on single operands. Three unary operators:
 Unary minus operator
 Increment operator (post and pre increment)
 Decrement operator (post and pre decrement)
 Unary minus operator(-)
Unary operator negates its value.For example, if a number ispositive then it becomes
negative when preceded with aunary minus operator.Similarly, if the number is negative,it
becomes positive after applying the unary minusoperator.
Ex: If x=10 and –x is – 10. Positive 10 becomes negative 10 after unary minus operation.

 Increment operator ++ and Decrement operator - -


The increment operator is a unary operator that increases the value of its operand by 1
and the decrementoperator decreases the value of its operand by 1. Forexample, --x is
equivalent to writing x = x – 1 and ++ x is equivalent to x = x+1
The increment/decrement operators have two variants prefix and postfix:
Prefix (++x or --x):
o The operator is applied before an operand is fetchedfor computation and, thus, the
altered value is used for the computation of the expression in which it occurs.

int x = 10, y; int x = 10, y;


y = ++x; y = - -x;
is equivalent to writing is equivalent to writing
x= x + 1; // x=11 x= x - 1; // x=9
y = x; // altered value 11 is assigned to y = x; // altered value 9 is assigned to
y y

Postfix(x++ or x- -):
o The operator is applied after an operand is fetched for computation. Therefore, the
unaltered value is used for the computationof the expression in which it occurs.
int x = 10, y; int x = 10, y;
y = x++; y = x- -;
is equivalent to writing is equivalent to writing
y = x; y = x;
x= x + 1; x= x - 1;

Note:
 The unary operators have a higher precedence than the binary operators.
 x++ is not same as ++x similarly x- - is not same as - - x.
 Unary operators are evaluated from right to left.

Conditional operator or ternary (?:)


It is just like an if-else statement that can be used within expressions.It is useful in
situations in which there are two or more alternatives for an expression. Some time it
replaces if-else condition phrases.
Syntax of the conditional operator is exp1 ? exp2 : exp3
o exp1 is evaluated first. If it is true, then exp2 is evaluated and becomes the result of the
expression
o Otherwise exp3 is evaluated and becomes the result of the expression.
Ex: large = (a > b) ? a : b
• Find the larger of two given numbers.
• First exp1, that is (a> b) is evaluated. If a is greater than b, then large = a
• else large = b. Hence, large is equal to either a or b but not both.
Ex: int a = 5, b = 3, c = 7, small;
small = (a <b ? (a <c ? a : c) : ( b < c ? b : c));
Bitwise Operators:
Bitwise operators are those operators that perform operations at bit level.Bitwise AND, bitwise
OR, bitwise XOR, and shift operators.Bitwise operators cannot be applied to float or double
variables.
Bitwise AND
When we use the bitwise AND operator (&), the bit in the first operand is ANDed with
the corresponding bit in the second operand.The bitwise AND operator compares eachbit
of its first operand with the corresponding bit of itssecond operand. If both bits are 1, the
corresponding bit inthe result is 1 and 0 otherwise.
Truth Table

If both bits are 1, the corresponding bit in the result is 1 and 0 otherwise.
Ex: 10101010 & 01010111 = 00000000
C Programme Code:
Ex: int a = 170, b = 85, c;
c = a&b;
1 0 1 0 1 0 1 0

& 0 1 0 1 0 1 0 1

Resul 0 0 0 0 0 0 0 0
t

Bitwise OR:
When we use the bitwise OR operator (|), the bit in the first operand is ORed with the
corresponding bit in the second operand.
The truth table:

If one or both bits are 1, the corresponding bit in the result is 1 and 0 otherwise.
Ex: For example, 10101010 & 01010101 = 11111111
1 0 1 0 1 0 1 0 C Programme Code:
& 0 1 0 1 0 1 0 1 int a = 170, b =85, c;
Result 1 1 1 1 1 1 1 1 c = a|b // output is 255
Bitwise NOT (~):
The bitwise NOT, or complement, is a unary operator that performs logical negation on
each bit of the operand. By performing negation of each bit, it actually produces the 1s
complement of the given binary value. Bitwise NOT operator sets the bit to 1 if it was
initially 0 and sets it to 0 if it was initially 1.
Ex: ~10101011 = 01010100
Bitwise XOR(^):
The bitwise XOR operator (^) performs operation on individual bits of the operands.
When the bitwise XOR operator is used, the bit in the first operand is XORed with the
corresponding bit in the second operand.
Truth Table is

C Programme Code:
int a = 10, b = 20, c=0;
c = a^b;
If one of the bits is 1, the corresponding bit in the result is 1 and 0 otherwise.

Ex: 10101010 ^ 01010101 = 11111111


1 0 1 0 1 0 1 0

^ 0 1 0 1 0 1 0 1

Result 1 1 1 1 1 1 1 1

You might also like