0% found this document useful (0 votes)
18 views14 pages

Operators

The document provides an overview of operators and expressions in the C programming language, detailing various categories of operators including arithmetic, assignment, increment/decrement, relational, logical, conditional, comma, sizeof, and bitwise operators. It explains the functionality and usage of these operators with examples, emphasizing the importance of operator precedence and associativity in evaluating expressions. Additionally, it covers concepts like integer, floating-point, and mixed mode arithmetic, as well as the role of parentheses in altering the order of operations.

Uploaded by

Nipun Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views14 pages

Operators

The document provides an overview of operators and expressions in the C programming language, detailing various categories of operators including arithmetic, assignment, increment/decrement, relational, logical, conditional, comma, sizeof, and bitwise operators. It explains the functionality and usage of these operators with examples, emphasizing the importance of operator precedence and associativity in evaluating expressions. Additionally, it covers concepts like integer, floating-point, and mixed mode arithmetic, as well as the role of parentheses in altering the order of operations.

Uploaded by

Nipun Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Operators And Expressions

An operator specifies an operation to be performed that yields a value. The variables, constants can be
joined by various operators to form an expression. An operand is a data item on which an operator acts.
Some operators require two operands, while others act upon only one operand. C includes a large
number of operators that fall under several different categories, which are as-

1. Arithmetic operators
2. Assignment operators .
3. Increment and Decrement operators
4. Relational operators.
5. Logical operators
6. Conditional operator
7. Comma operator
8. sizeof operator
9. Bitwise operators
10. Other operators

Arithmetic Operators

Arithmetic operators are used for numeric calculations. They are of two types - .

1. Unary arithmetic operators

2. Binary arithmetic operators

Unary Arithmetic Operators


Unary operators require only one operand. For example-

+x -y

Here '-' changes the sign of the operand y.

Binary Arithmetic Operators

Binary operators require two operands. There are five binary arithmetic operators-

% (modulus operator) cannot be applied with floating point operands. There is no exponent operator in C.
However there is a library function pow( ) to carry out exponentiation operation.
Integer Arithmetic
When both operands are integers then the arithmetic operation with these operands is called integer arithmetic and
the resulting value is always an integer. Let us take two variables a and b. The value of a = 17 and b = 4, the results
of the following operations are-

After division operation the decimal part will be truncated and result is only integer part of quotient. After modulus
operation the result will be remainder part of integer division. The second operand must be nonzero for division
and modulus operations.

Floating-Point Arithmetic

When both operands are of float type then the arithmetic operation with these operands is called floating- point
arithmetic: Let us take two variables a and b. The value of a = 12.4 and b = 3.1 the result of the following operations
are as-

Mixed Mode Arithmetic

When one operand is of integer type and the other is of floating type then the arithmetic operation wi1l these
operands is known as mixed mode arithmetic and the resulting value is float type.

If a = 12, b = 2.5

Sometimes mixed mode arithmetic can help in getting exact results. For example the result of expression 5/2 will
be 2, since integer arithmetic is applied. If we want exact result we can make one of the operand float type. For
example 5.0/2 or 5/2.0, both will give result 2.5.
Assignment Operators

A value can be stored in a variable with the use of assignment operator. This assignment operator “=” , is used in
assignment expressions and assignment statements.

The operand on the left hand side should be a variable, while the operand on the right hand side can any variable,
constant or expression. The value of right hand operand is assigned to the left hand operand. Here are some
examples of assignment expressions-

x=8 /* 8 is assigned to x */

y=5 /* 5 is assigned to y */

s = x+y-2 /* Value of expression x+y-2 is assigned to s */

y=x /* Value of x is assigned to y */

x=y /* Value of y is assigned to x */

The value that is being assigned is considered as value of the assignment expression. For example = 8 is an
assignment expression whose value is 8.

We can have multiple assignment expressions also, for example-

x = y = z = 20

Here all the three variables x, y, z will be assigned value 20, and the value of the whole expression will be 20.

When the variable on the left hand side of assignment operator also occurs on right hand side then we can avoid
writing the variable twice by using compound assignment operators. For example-

x=x+5

can also be written as-

x += 5

Here += is a compound assignment operator.

Increment And Decrement Operators

C has two useful operators increment ( ++ ) and decrement ( - - ). These are unary operators because they operate
on a single operand. The increment operator ( ++ ) increments the value of the variable by 1 and decrement
operator ( - - ) decrements the value of the variable by 1.

++x is equivalent to x+1


++x is equivalent to x+1

These operators should be used only with variables; they can't be used with constants or expressions. For example
the expressions ++5 or ++(x+y+z) are invalid.

These operators are of two types-

1. Prefix increment / decrement - operator is written before the operand (e.g. ++x or - -x )
2. Postfix increment / decrement - operator is written after the operand (e.g. x++ or x - - )

Prefix Increment / Decrement

Here first the value of variable' is incremented / decremented then the new value is used in the operation Let us
take a variable x whose value is 3.

The statement y = ++x; means first increment the value of x by 1, then assign the value of x to y This single
statement is -equivalent to these two statements-

x= x+l;

y= x;

Hence now value of x is 4 and value of y is 4.

The statement y = - -x ; means first decrement the value of x by 1 then assign the value of x to ) This statement is
equivalent to these two' statements.

x = x -1;

y= x;

Hence now value of x is 3 and value of y is 3.

Postfix Increment / Decrement

Here first the value of variable is used in the operation and then increment/decrement is performed. Let us take a
variable whose value is 3.

The statement y = x++; means first the value of x is assigned to y and then x is incremented. The statement is
equivalent to these two statements-

y = x;

x = x+1;

Hence now value of x is 4 and value of y is 3.

The statement y = x- -; means first the value of x is assigned to y and then x is decremented. This statement is
equivalent to these two statements-

y = x;

x = x-1;
Hence now value of x is 3 and value of y is 4.

Relational Operators
Relational operators are used to compare values of two expressions depending on their relations. An expression
that contains relational operators is called relational expression. If the relation is true "then the value of relational
expression is 1 and if the relation is false then the value of expression is O. The relational operators are-

Let us take two variables a =9 and b =5, and form simple relational expressions with them-

The relational operators are generally used in if.. .else construct and loops. In our next program we'll use the if
statement to illustrate the use of relational operators. The if control statement evaluates an expression, and if this
expression is true (non-zero) then the next statement is executed, otherwise the next statement is skipped.

Logical Or Boolean Operators


An expression that combines two or more expressions is termed as a logical expression. For combining these
expressions we use logical operators. These operators return 0 for false and 1 for true. The operands may be
constants, variables or expressions. C has three logical operators.

Here logical NOT is a unary operator while the other two are binary operators. Before studying these operators
let us understand the concept of true and false. In C any non-zero value is regarded as true and zero is regarded
as false.
AND ( &&) Operator

This operator gives the net result true if both the conditions are true, otherwise the result is false.

Let us take three variables a = 10, b = 5, c = 0

Suppose we have a logical expression-

(a==10)&&(b<a)
Here both the conditions a = = 10 and b < a are true, and hence this whole expression is true. Since the logical
operators return 1 for true hence the value of this expression is 1.

In the last two expressions we have taken only variables. Since nonzero values are regarded as true and zero
value is regarded as false, so variables a and b are considered true and variable c is considered false.

OR ( II)Operator
This operator gives the net result false, if both the conditions have the value false, otherwise the result is true.

Let us take. three variables a = 10, b = 5, c =.0

Consider the logical expression-

(a >= b) II(b > 15)


This gives result true because one condition is true.
Not ( ! ) Operator

This is a unary operator and it negates the value of the condition. If the value of the condition is false then it gives
the result true. If the value of the condition is true then it gives the result false. Let us take three variables a = 10,
b = 5, c =0. Suppose we have this logical expression- ! ( a = = 10 )

The value of the condition (a= =10) is true. NOT operator negates the value of the condition. Hence the result is
false.

Conditional Operator
Conditional operator is a ternary operator ( ? and: ) which requires three expressions as operands. This written
as-

TestExpression ? expression1 : expression2

Firstly the TestExpression is evaluated.

If TestExpression is true(nonzero), then expression1 is evaluated and it becomes the value of the overall
conditional expression.

If TestExpression is false(zero), then expression2 is evaluated and it becomes the value of overall conditional
expression.

example consider this conditional expression-

a>b?a:b

Here first the expression a > b is evaluated,if the value is true then the value of variable a becomes value of
conditional expression otherwise the value of b becomes the value of conditional expression.

Suppose a = 5 and b = 8, and we use the above conditional expression in a statement as-

max = a >b ? a : b;

First the expression a > b is evaluated, since it is false so the value of b becomes the value of conditional
expression and it is assigned to variable max.

In our next example we have written a conditional statement by putting a semicolon after the conditional
expression.
a < b ? printf("a is smaller") : printf("b is smaller");

Since the expression a < b is true, so the first printf function is executed.

Comma Operator
The comma operator ( , ) is used to permit different expressions to appear in situations where only one expression
would be used. The expressions are separated by the comma operator. The separated expressions are evaluated
from left to right and the type and value of the rightmost expression is the type and value of the compound
expression.

For example consider this expression-

a= 8, b= 7, c=9, a+b+c

Here we have combined 4 expressions. Initially 8 is assigned to the variable a, then 7 is assigned to the variable
b, 9 is assigned to variable c and after this a+b+c is evaluated which becomes the value of whole expression. So
the value of the above expression is 24. Now consider this statement-

sum = ( a = 8, b = 7, c = 9, a+b+c );

Here the value of the whole expression on right side will be assigned to variable sum i.e. sum will be assigned
value 24. Since precedence of comma operator is lower than that of assignment operator hence the parentheses
are necessary here. The comma operator helps make the code more compact, for example without the use of
comma operator the above task would have been done in 4 statements.

sizeof Operator
sizeof is an unary operator. This operator gives the size of its operand in terms of bytes. The operand can be a
variable, constant or any datatype( int, float, char etc ). For example sizeof(int) gives the bytes occupied by the
int datatype i.e., 2.
Generally sizeof operator is used to make portable programs i.e. programs that can be run on different machines.
For example if we write our program assuming int to be of 2 bytes, then it won't run correctly on a machine on
which int is of 4 bytes. So to make general code that can run on all machines we can use sizeof operator.

Bitwise Operators
C has the ability to support the manipulation of data at the bit level. Bitwise operators are used for operations on
individual bits. Bitwise operators operate on integers only. The bitwise operators are as-

Bitwise AND ( & )


It is a binary operator and requires two operands. These operands are compared bitwise i.e. all the

corresponding bits in both operands are compared. The resulting bit is 1, only when the bits in both operands are
1, otherwise it is O.

Bitwise OR ( I )
The corresponding bits of both operands are compared and the resulting bit is 0, only when the bits in both
operands are 0, otherwise it is 1.
Bitwise XOR ( ^ )
The corresponding bits of both operands are compared and the resulting bit is 1, if bits of both operands have
different value, otherwise it is O.

One's Complement ( ,.., )


One's complement operator -is a unary operator and requires only one operand. It negates the value c the bit. If
the bit of the operand is 1 then the resulting bit is 0 and if the bit of the operand is 0 the resulting bit is 1.
Bitwise Left Shift ( « )
This operator is used for shifting the bits left. It requires two operands. The left operand is the operand whose
bits are shifted and the right operand indicates the number of bits to be shifted. On shifting the bits left, an equal
number of bit positions on the right are vacated. These positions are filled in with 0 bits. Let us take an integer
variable a = 0x1346. The binary representation of x is-

Bitwise Right Shift ( » )


This operator is similar to the left shift operator, except that it shifts the bits to the right side. On shifting

bits right, an equal number of bit positions on the left are vacated. These positions are filled in with its in
unsigned integers. We'll again take a variable a = 0x1346, and this time we'll find out a » 4.

In right shift if the first operand is a signed integer, then the result is compiler dependent. Some compilers follow
logical shift while others may follow arithmetic shift. Logical shift - The vacated bits are always filled with zeros.
Arithmetic shift - The vacated bits are filled with the value of the leftmost bit in the initial bit pattern. If the
leftmost bit is 1, then the vacated positions are filled with 1, and if the leftmost bit is 0, then the vacated positions
are filled with 0. The following two examples show arithmetic shift in signed integers. In first case the leftmost
bit is 1 so the vacated bits are filled with 1, and in the second case the leftmost bit is 0, so the vacated bits are
filled with 0.
Actually the leftmost bit represents the sign bit. If the number is negative, then it is set to 1. So .~ other words we
can say that, in arithmetic shift the vacated bits are filled with the sign bit.

Precedence And Associativity Of Operators

Let us take some expressions and see how they will be evaluated-

(i) x=a + b <c

Here + operator has higher precedence than < and =, and < has more precedence than =, so first a+b
will be evaluated, then < operator will be evaluated, and at last the whole value will be assigned to x.
If initial values area=2,b=6,c=9,then final value of x will be 1.
(ii) x *= a + b

Here + operator has higher precedence than *=, so a+b will be evaluated before compound assignment. This is
interpreted as x = x*(a+b) and not as x = x*a+b. If initial values are x = 5, a = 2, b = 6, then final value of x will
be 13.

(iii) x = a<=b II b==c


Here order of evaluation of operators will be <=, = =, 11,=. If initial values are a = 2, b = 3, c = 4, then final
value of x will be 1.

Role Of Parentheses- In Evaluating Expressions


If we want to change the order of precedence of any operation, we can use parentheses. According to the
parentheses rule, all the operations that are enclosed within parentheses are performed first.

For example in expression 24/2+4, division will take place before addition according to precedence rule ( but if
we enclose 2+4 inside parentheses then addition will .be performed first: So the value of expression 24/(2+4) is
4.

For evaluation of expression inside parentheses same precedence and associativity rules apply. Fe example- .

(22 - 4 ) / (2+4*2-1)

Here inside parentheses multiplication will be performed before addition.

There can be nesting of parentheses in expressions i.e. a pair of parentheses can be enclosed with another pair of
parentheses. For example- .

( 4* (3+2) ) / 10

In these cases, expressions within innermost parentheses are always evaluated first, and then ne innermost
parentheses and so on, till outermost parentheses. After evaluation of all expressions with parenthesis, the
remaining expression is evaluated as usual. In the above expression . 3+2 is evaluate first and then 4*5 and then
20/10.

Sometimes in complex expressions, parentheses are used just to increase readability. For example comp: the
following two expressions-

You might also like