Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit - 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

UNIT – 3 Expression & Operations

C Language

Introduction of Operators & Expression:


Operators are the special symbols that report the computer which type of
arithmetic or logical operation to be performed on variable or operands
specifying in expression. We have already used mathematic & relational
operators for performing calculation like +, -, *, / etc. There are many types of
operators available in C language that is listed in following table.

Sr Operator Name Operator Symbol


No.
1 Arithmetic Operators +,-,*,/,%
2 Assignment Operators =
3 Relational Operators <,<=,>,>=,==,!=
4 Logical Operators &&,||,!
5 Conditional or Ternary ?:
Operator
6 Increment & Decrement ++,--
Operators
7 Bitwise Operators & , | ,^ ,>> ,<<
8 Special Operators Size of operator, pointer
operators(&, *)

Now consider following example that illustrates the concept of Expression,


Operators and Operands.
Z=X+Y
In above given example, X , Y and Z are the Operands and =, + are the
operators. So we can define expression & Operands as under.

An Expression is a combination or sequence of Operands as well as


Operators.
An Operand is a constant value or a variable on which the operation to
be performed according to operator specified in the expression.

(1) Arithmetic Operators


The C Language provides all types of basic arithmetic operators. Every
operator is used for performing specific type of calculation. All arithmetic
operators are listed in following table with example.

Consider expression Z=X+Y


Here, Z, X and Y are variables or operands, ‘+’ is operator, there operator
may be ‘*’, ‘/’, ‘%’.
The result of arithmetic expression is depending up on type of operands.
According to type of operands we can classify expression in following
different categories.
SDJ International College Page 1
UNIT – 3 Expression & Operations

(2) Assignment Operator


The Assignment Operator evaluates expressions on the right of it and
substitutes its result on the left of the expression. The assignment operator
is simple equal sign(=). The assignment operator is used to assign a value
to a variable. The general format of an assignment operator statement is:
Variable Name = Expression;
The expression can be a single variable or a literal, o it may contain
variables, literals & operators. The sequence of assign a value always from
right to left.
For example:
X=50; //50 is assign to variable X
X=50*10; //First calculates multiply and store its answer 500 to X
variable.

We can write a shorthand form of assigning value to a variable:


For example: X+=Y is similar with expression: X=X+y. These types of
operators are called compound assignment operators. Similarly way we
can use assignment operator to combine with the major arithmetic
operators such as: +, -,*, / and %. The following table illustrates other
examples:

Simple Assignment Statement Shorthand Method


X=X-1 X-=1
X=X*(n+1) X*=(n+1)
X=X/(n+1) X/=(n+1)
X=X%Y X%=Y

(3) Relational Operators


Sometimes we required to compare value of two operands and based on
that to perform next execution step in such case relational operators are
used to compare values of operands and give result either true or false.

Generally these operators are used for checking condition or with if....else
statement & looping statements in C programs. The logical or conditional
statement expression is either return True or False, and False is considered
as 0 and True as a 1.

The following table contains list of relational operators.


Relational Operator Meaning of Operators
< Is less than
<= Is less than or equal to
> Is greater than
>= Is greater than or equal to
== Is equal to
!= Is not equal to

SDJ International College Page 2


UNIT – 3 Expression & Operations

General form for writing relational operator like


exp1 < Relational Operator > exp2
Now consider X=9 and Y=3 expression which returns different result in
relational operators.

Expression Logical Value Expression Value


X<Y FALSE 0
X<=Y FALSE 0
X>Y TRUE 1
X>=Y TRUE 1
X==Y FALSE 0
X!=Y TRUE 1

(4) Logical Operators


Whenever to require check more than one conditions at time using single
statement in such case, we can take the help of Logical operators. C
language offers three types of logical operators.

Operator Name
&& AND
|| OR
! NOT

(A) AND (&&) Operator


This operator is used to check two conditions or expressions with
relational operators simultaneously. If both the expressions to the left
and to the right of the logical operator is true then the whole condition
become true. It means if all the conditions are satisfied then and then it
will return true other wise if one of the condition becomes false then it
will return False.

For example: Consider the following Truth table for AND


operator which return True (1) and False (0)
Expression1 / Conditions1 Expression2 / Condition2 Result
True True True
True False False
False True False
False False False

(B) OR (||) Operator


The logical OR (||) operator is used to check more than one condition at
a time and it returns only true if one of the condition is satisfied. If all
the conditions are wrong then and then it will return False otherwise
return true in all other cases.

SDJ International College Page 3


UNIT – 3 Expression & Operations

For example: Consider the following Truth table for OR operator


which return True (1) and False (0)

Expression1 / Condition1 Expression2 / Condition2 Result


TRUE TRUE TRUE
TRUE FALSE TRUE
FALSE TRUE TRUE
FALSE FALSE FALSE

(C) NOT (!) Operator


The Logical not operator takes only single expression and returns to
true if the expression is false and evaluates to false if the expression is
true. In short it returns the reverse result of an expression.

For example: Consider following Truth table of NOT operator.

Expression1 / Condition1 Result


True False
False True

(5) Conditional or Ternary Operator


A conditional operator is also known as a Ternary operator. Conditional
expression is made up with the help of (?:) Operators and that is one of the
special operators supported by the C language. The conditional operator
evaluates an expression and returns one of two values based on whether
condition is TRUE or FALSE. The general form of conditional operator is
given as under.
Condition ? Exp – 1 : Exp – 2;
Here conditional operator contains three parts. In first part specify
condition which is to be executed and if this condition is True then Exp-1
will be executed otherwise Exp-1 will be executed otherwise Exp-2 will be
executed.

For example: Consider following example:


Int a=5, b=6, c;
C=a>b? a:b;
Here C=6
In above given example, first check the condition, if a>b the condition
becomes false so it returns value of b variable.

(6) Increment & Decrement Operators


Increment and decrement operators are very useful operators. They are
also known as unary operators. The operator ++ means “add1” value to
variable and assign to same variable or “increment by 1”. Similarly
operator – means “subtract 1” from variable and update value in same
variable or “decrement by 1”. They are also equivalent to +=1 and -=1.
SDJ International College Page 4
UNIT – 3 Expression & Operations

For example:
Expression a=a+1 or a+=1 or a++
B=b-1 or b-=1 or b—

(7) Bitwise Operators


Using C language we can access and manipulate bits in variables, allowing
you to perform low level operations. C supports following types of bitwise
operators and they are listed in following table. Their precedence is lower
than arithmetic, relational and logical operators. Bitwise operators may not
be applied to float and double type of data.

Operators Meaning Precedence Associativity


& Bitwise AND ? Left to right
| Bitwise OR Lowest Left to right
~ 1’s complement Highest Left to right
^ Bitwise Exclusive OR ? Left to right
<< Shift left ? Left to right
>> Shift right ? Left to right

(8) Special Operators


C language support some of the important special operators such as sizeof,
comma Operator and pointer (&.*) operators.
(a) Size of operator
This operator is used to find out the memory size occupied by the
variable in memory. It returns a memory size in byte unit.
For example:
int x,y;
y=sizeof(x);
printf(“\n%d”,y);
Here output is 2 because x is an integer variable which occupies 2
bytes of memory.

(b) Comma operator


The comma operator is used to represent the multiple statements that
are related to a single statement. Generally this operator is used to
perform some of the arithmetic operations.
For example:
Int A, B, C;
A=10;
B=20;
C=A+B;
In above given three statement, the first two statement assign values to
variable and third statement perform addition operation on two variable
and store final answer into C variable.

SDJ International College Page 5


UNIT – 3 Expression & Operations

Same statement we can write in a single statement with the help of


comma operator:
C=(A=10.B=20.C=A+B);
The execution process in this statement is start from left to right. It
means first assign 10 to A, 20 to B and calculate sum and store its
value in C variable.

Similar type of comma operator we can use in for loop also.


Following example displays, use of comma operator in for loop.
For(x=1,y=1,x>y,x++,y--)
{
Statement blocks;
……………………..
}

(c) Pointer Operators


Pointer is a one type of variable which points address of another
variable. For that & operator indicates the address of operator and *
symbol indicates the value of the pointer variable.

Hierarchy of Operations
While executing an arithmetic statement, which has two or more
operators, we may have some problems as to how exactly does it get
executed.

Priority/ Operators Description Associativity


Precedence
1st ‘*’ / Multiplication, division, Left to right
% modular division
2nd + - Addition, subtraction Left to right
3rd = Assignment Left to right

Expressions:

Expression is the most fundamental element of C language. They are made


up of two atomic elements data and operator. C supports variety of data
types and large number of operators. In simplest terms expression is a
statement made up of data and operators operating upon that data which is
evaluated by compiler. Expression by definition is any valid combination
of operators, constants, functions and variables. Expressions in C language
are very developed and sophisticated allowing you to complete the
programming with uttermost ease and flexibility.

SDJ International College Page 6

You might also like