0% found this document useful (0 votes)
22 views46 pages

Expressions

Expressions consist of operands and operators that specify computations. An operand can be a variable, constant, function call, or macro. Expressions can be simple, with one operator, or compound, with multiple operators. Operators are classified as arithmetic, relational, logical, bitwise, or assignment. Precedence and associativity determine the order evaluations are performed.

Uploaded by

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

Expressions

Expressions consist of operands and operators that specify computations. An operand can be a variable, constant, function call, or macro. Expressions can be simple, with one operator, or compound, with multiple operators. Operators are classified as arithmetic, relational, logical, bitwise, or assignment. Precedence and associativity determine the order evaluations are performed.

Uploaded by

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

Expressions

• In general, a meaningful expression consists of one or


more operands and operators that specify the
operations to be performed on operands.
• For example, a = 2 + 3.
• Thus, an expression is a sequence of operands and
operators that specifies the computation of a value.
Operands
• An operand specifies an entity on which an operation is performed.
• An operand can be a variable name, a constant, a function call or a
macro name.
• For example, a = printf(“Hello”) + 2 is a valid expression involving
three operands, namely a variable name, i.e. a, function call, i.e.
printf(“Hello”) and a constant, i.e. 2.
Operators
Simple and Compound Expression
• An expression that has only one operator is known as
the simple expression while an expression that involves
more than one operator is called a compound
expression.
• a+2 is a simple expression and b=2+3*5 is a compound
expression.
• In compound expression, while evaluating, one must
determine the order in which operators will operate
(depends upon the precedence and the associativity).
Classification based on role of operator
• Based upon their role, operators are classified as,
• Arithmetic operators
• Relational operators
• Logical operators
• Bitwise operators
• Assignment operators
• Miscellaneous operators

5
Arithmetic operators

6
Example Program
#include <stdio.h>
int main()
{
int a;
a = 2 * 3.25;
printf("the result of evaluation is %d",a);
return 0;
}

7
Example Program
#include <stdio.h>
int main()
{
int a;
a = 4 % 3;
printf("the value of a is %d",a);
return 0;
}

10
Relational operators

12
Example Program
#include <stdio.h>
int main()
{
int a;
a=2<3;
printf("the value of a is %d",a);
return 0;
}

Note: Relational operators will produce 1 if the condition is TRUE else it will produce 0.

14
Logical operators

15
Logical operators
• Expressions connected by the logical AND (&&) or the
logical OR (||) operator are evaluated left to right and
evaluation stops as soon as truthfulness or falsehood
of the expression is determined. Thus in an
expression:
• E1&&E2, where E1 and E2 are sub-expressions, E1 is
evaluated first. If E1 evaluates to 0, E2 will not be
evaluated and the result of the overall expression will
be 0. If E1 evaluates to a non zero value, then E2 will
be evaluated to determine the truth value of the
overall expression.
16
Example Program
#include <stdio.h>
int main()
{
int i=0, j=1,n;
n=i&&j;
printf("resultant values after evaluation are:");
printf("%d %d %d ",i,j,n);
return 0;
}
19
Logical operators
• E1||E2, where E1 and E2 are sub-expressions, E1 is
evaluated first.
• If E1 evaluates to a non zero value, E2 will not be
evaluated and the result of the overall expression will
be 1.
• If E1 evaluates to 0, then E2 will be evaluated to
determine the truth value of the overall expression.

23
Example Program
#include <stdio.h>
int main()
{
int i=0, j=1, n;
n=i||j;
printf("resultant values after evaluation are:");
printf("%d %d %d ",i,j,n);
return 0;
}
26
Assignment operators

27
Increment & Decrement operators

28
Example Program
#include <stdio.h>
int main()
{
int i=2,j=2,k,n;
k=++i;
n=j++;
printf("i=%d, j=%d, k=%d, n=%d",i,j,k,n);
return 0;
}
30
Example Program
#include <stdio.h>
int main()
{
int i=2,j=2,k,n;
k=--i;
n=j--;
printf("i=%d, j=%d, k=%d, n=%d",i,j,k,n);
return 0;
}
31
Bitwise operators

32
Miscellaneous operator
• Function call operator ()
• Array subscript operator []
• Member select operator i) . ii) ->
• Indirection operator *
• Conditional operator
• Comma operator ,
• Sizeof operator
• Address of operator &

37
Conditional operator

38
Example Program
#include <stdio.h>
int main()
{
int age=19;
(age>=18)? (printf("eligible for voting")) : (printf("not
eligible for voting"));
return 0;
}

40
sizeof() operator
• Sizeof is a much used operator in the C or C++.
• It is a compile time unary operator which can be used to compute the
size of its operand.
Precedence & Associativity of operators
• Certain operators have higher precedence than others;
for example, the multiplication operator has a higher
precedence than the addition operator.
• For example, x = 7 + 3 * 2; here, x is assigned 13, not 20
because operator * has a higher precedence than +, so
it first gets multiplied with 3*2 and then adds into 7.
• Operators Associativity is used when two operators of
same precedence appear in an expression. Associativity
can be either Left to Right or Right to Left.
• For example: ‘*’ and ‘/’ have same precedence and
their associativity is Left to Right, so the expression
“100 / 10 * 10” is treated as “(100 / 10) * 10”.
Example

You might also like