0% found this document useful (0 votes)
15 views77 pages

Unit 2

The document provides an overview of operators and expressions in C programming, detailing various types of operators such as arithmetic, relational, logical, and assignment operators. It explains operator precedence and associativity, along with examples of how to use these operators in C code. Additionally, it includes practical examples demonstrating the functionality of different operators and their usage in conditional statements.

Uploaded by

nayna sawant
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)
15 views77 pages

Unit 2

The document provides an overview of operators and expressions in C programming, detailing various types of operators such as arithmetic, relational, logical, and assignment operators. It explains operator precedence and associativity, along with examples of how to use these operators in C code. Additionally, it includes practical examples demonstrating the functionality of different operators and their usage in conditional statements.

Uploaded by

nayna sawant
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/ 77

Operators & Expressions

SUBJECT: PROGRAMMING FUNDAMENTALS USING C


Definition

“An operator is a symbol (+,-,*,/) that directs the


computer to perform certain mathematical or logical
manipulations and is usually used to manipulate
data and variables”
Ex: a+b
Operators in C
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators
Arithmetic operators
Operator example Meaning
+ a+b Addition –unary
- a–b Subtraction- unary
* a*b Multiplication
/ a/b Division
% a%b Modulo division- remainder
ber2, sum; printf("Enter two integers: "); scanf("%d %d", &number1, &number2); // calculate the sum sum = number1 + number2; printf("%d +

include <stdio.h>
 int main()
{
 int number1, number2, sum;
printf("Enter two integers: ");
 scanf("%d %d", &number1, &number2);
// calculate the sum
 sum = number1 + number2;
 printf("%d + %d = %d", number1, number2,
sum); return 0;
}
 #include<stdio.h>
int main()
{
int num1,num2,div;
printf("\tEnter Two Numbers\n");
printf("---------------------------\n");
printf("Enter First Number : ");
scanf("%d", &num1);
printf("\nEnter Second Number : ");
scanf("%d",&num2);
div=num1/num2;
printf("\nDivision of %d & %d is =
%d",num1,num2,div);
return 0;
}
Modulus Example:

#include <stdio.h>
 int main()
{
int x=10;
int y=3;
int result = 10 % 3;
printf("%d\n", result); // Output: 1 return
0; }
Operator Precedence and Associativity
in C:

The concept of operator precedence and


associativity in C helps in determining which
operators will be given priority when there are
multiple operators in the expression. It is very
common to have multiple operators in C language
and the compiler first evaluates the operater with
higher precedence. It helps to maintain the
ambiguity of the expression and helps us in
avoiding unnecessary use of parenthesis.
Category Operator Associativity

Postfix () [] -> . ++ - - Left to right

Unary + - ! ~ ++ - - (type)* & sizeof Right to left

Multiplicative */% Left to right

Additive +- Left to right

Shift << >> Left to right

Relational < <= > >= Left to right

Equality == != Left to right

Bitwise AND & Left to right

Bitwise XOR ^ Left to right

Bitwise OR | Left to right

Logical AND && Left to right

Logical OR || Left to right

Conditional ?: Right to left

= += -= *= /= %=>>= <<= &= ^= |


Assignment Right to left
=

Comma , Left to right


In the following example −
15 / 5 * 2
Both the "/" (division) and "*" (multiplication)
operators have the same precedence, so the
order of evaluation will be decided by
associativity.
As per the above table, the associativity of
the multiplicative operators is from Left to
Right. So, the expression is evaluated as −
(15 / 5) * 2
It evaluates to −
3 * 2 = 6
 The left−to−right associativity of multiplicative operator
results in multiplication of "b" and "c" divided by "e". The
result then adds up to the value of "a".
 #include <stdio.h>
 int main()
{
 int a = 20;
 int b = 10;
 int c = 15;
 int d = 5;
 int e;
 e = a + b * c / d;
 printf("e : %d\n" , e );
 return 0;
}
 e: 50
 We can use parenthesis to change the order of evaluation.
Parenthesis () got the highest priority among all the C
operators.
#include <stdio.h>
int main()
{
int a = 20;
 int b = 10;
 int c = 15;
int d = 5;
 int e;
e = (a + b) * c / d;
printf("e: %d\n", e);
 return 0;
}
Output e: 90
In the expression that calculates e, we have
placed a+b in one parenthesis, and c/d in
another, multiplying the result of the two.
#include <stdio.h>
int main()
{
int a = 20;
int b = 10;
 int c = 15;
int d = 5;
int e;
e = (a + b) * (c / d);
printf("e: %d\n", e );
return 0;
}
Relational Operators

Operator Meaning
< Is less than
<= Is less than or equal to
> Is greater than
>= Is greater than or equal
to
== Equal to
!= Not equal to
// Working of relational operators
 #include <stdio.h>
 int main() {
 int a = 5, b = 5, c = 10;
 printf("%d == %d is %d \n", a, b, a == b);
 printf("%d == %d is %d \n", a, c, a == c);
 printf("%d > %d is %d \n", a, b, a > b);
 printf("%d > %d is %d \n", a, c, a > c);
 printf("%d < %d is %d \n", a, b, a < b);
 printf("%d < %d is %d \n", a, c, a < c);
 printf("%d != %d is %d \n", a, b, a != b);
 printf("%d != %d is %d \n", a, c, a != c);
 printf("%d >= %d is %d \n", a, b, a >= b);
 printf("%d >= %d is %d \n", a, c, a >= c);
 printf("%d <= %d is %d \n", a, b, a <= b);
 printf("%d <= %d is %d \n", a, c, a <= c);
 return 0; }
5 == 5 is 1
5 == 10 is 0
 5 > 5 is 0
5 > 10 is 0
 5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
 5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1
 // C program to demonstrate working of relational operators
 #include <stdio.h>
 int main()
{
 int a = 10, b = 4;
 // greater than example
 if (a > b)
 printf("a is greater than b\n");
 else
 printf("a is less than or equal to b\n");
 // greater than equal to
 if (a >= b)
 printf("a is greater than or equal to b\n");
 else
 printf("a is lesser than b\n");

 // less than example
 if (a < b)
 printf("a is less than b\n");
 else
 printf("a is greater than or equal to b\n");
 // lesser than equal to
 if (a <= b)
 printf("a is lesser than or equal to b\n");
 else
 printf("a is greater than b\n");
 // equal to
 if (a == b)
 printf("a is equal to b\n");
 else
 printf("a and b are not equal\n");
 // not equal to
 if (a != b)
 printf("a is not equal to b\n");
 else
 printf("a is equal b\n");
 return 0;
}
int main() {
int a = 5;
int b = 7;
int c = 5;

printf("a == b: %d\n", a == b); //use equal to
operator for a and b
printf("a == c: %d\n", a == c); //use equal to
operator for a and c

return 0;
}
Output:
a == b: 0
a == c: 1
Then, we define the main() function, which
serves as the entry point of the program.
Inside the main, we declare
three integer variables, a, b, and c, and assign
them the values 5, 7, and 5, respectively.
Next, as mentioned in code comments, we use
the equal to operator in expression a==b to
compare a and b and the printf() function to
print the comparison result.
We also use the %d format specifier to specify
that the value must be replaced with an integer
and the newline escape sequence to shift the
cursor to the next line once the output is printed.
The output is a == b: 0 because 5 is not equal to
7.
Similarly, we compare a to c and print the result
of the comparison, which is a == c: 1 since 5 is
equal to 5.
Output:a is greater than b
a is greater than or equal to b
a is greater than or equal to b
a is greater than b
a and b are not equal
a is not equal to b
#include <stdio.h>

int main() {
int a = 5;
int b = 7;
int c = 5;
printf("a != b: %d\n", a != b);
printf("a != c: %d\n", a != c);
return 0;
}
Output:
a != b: 1
a != c: 0
There are three integer variables, a,
b, and c, with values 5, 7, and 5, respectively.
We use the not equal to operator inside
the printf() function to compare a to b and
print the comparison result. The output is a !
= b: 1 because 5 is not equal to 7, and the !=
operator checks for inequality, resulting in a
true condition (1).
Similarly, we use the relational operator in
the expression a!=c and the printf() function
to compare and display the result. The output
is a != c: 0 since 5 is equal to 5, and the !=
operator checks for inequality and returns
false, i.e. 0.
The program culminates execution with a
return 0 statement.
Logical Operators
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT

Logical expression or a compound relational


expression-
An expression that combines two or more
relational expressions
Ex: if (a==b && b==c)
Truth Table
Value of the expression
a b
a && b a || b
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 1
 // C program for Logical
 // AND Operator
 #include <stdio.h>
 // Driver code
 int main()
{
 int a = 10, b = 20;
 if (a > 0 && b > 0)
{
 printf("Both values are greater than 0\n");
 } else
 {
 printf("Both values are less than 0\n");
}
 return 0;
 }
Output:
Both values are greater than 0
 // C program for Logical //
 OR Operator
 #include <stdio.h>
 // Driver code
 int main()
 {
 int a = -1, b = 20;
 if (a > 0 || b > 0)
 {
 printf("Any one of the given value is " "greater than 0\n");
 }
 else
{
 printf("Both values are less than 0\n");
 } return 0;
 }
Output:Any one of the given value is greater
than 0
// C program for Logical
// NOT Operator
#include <stdio.h>
// Driver code
int main()
{
int a = 10, b = 20;
if (!(a > 0 && b > 0))
{
// condition returned true but
// logical NOT operator changed
// it to false
printf("Both values are greater than 0\n");
} else
{ printf("Both values are less than 0\n");
}
return 0;
}
Assignment operators
Syntax:
v op = exp;
Where v = variable,
op = shorthand assignment operator
exp = expression
Ex: x=x+3
x+=3
o Assignment operators are used for assigning
value to a variable. The left side operand of
the assignment operator is a variable and
right side operand of the assignment operator
is a value. The value on the right side must be
of the same data-type of the variable on the
left side otherwise the compiler will raise an
error.
o Different types of assignment operators are
shown below:
o “=”: This is the simplest assignment operator. This
operator is used to assign the value on the right to the
variable on the left. Example:
o a = 10;
b = 20;
ch = 'y';
ators. This operator first subtracts the value on the right from the current value of the variable on left and then a

+=”: This operator is combination of ‘+’ and ‘=’


operators. This operator first adds the current
value of the variable on left to the value on the
right and then assigns the result to the variable
on the left. Example:
(a += b) can be written as (a = a + b)
If initially value stored in a is 5. Then (a += 6) =
11.

“-=” This operator is combination of ‘-‘ and ‘=’


operators. This operator first subtracts the value
on the right from the current value of the
variable on left and then assigns the result to the
variable on the left. Example:
(a -= b) can be written as (a = a - b)
If initially value stored in a is 8. Then (a -= 6) =
 “*=” This operator is combination of ‘*’ and ‘=’
operators. This operator first multiplies the
current value of the variable on left to the value
on the right and then assigns the result to the
variable on the left. Example:
(a *= b) can be written as (a = a * b)
If initially value stored in a is 5. Then (a *= 6) =
30.
 “/=” This operator is combination of ‘/’ and ‘=’
operators. This operator first divides the current
value of the variable on left by the value on the
right and then assigns the result to the variable
on the left. Example:
(a /= b) can be written as (a = a / b)

If initially value stored in a is 6. Then (a /= 2) =


 //C program to demonstrate
 // working of Assignment operators
 #include <stdio.h>
 int main() {
 int a = 10; // Assigning value 10 to a // using "=" operator
 printf("Value of a is %d\n", a);
 // Assigning value by adding 10 to a
 // using "+=" operator
 a += 10;
 printf("Value of a is %d\n", a);
 // Assigning value by subtracting 10 from a
 // using "-=" operator
 a -= 10;
 printf("Value of a is %d\n", a);
 // Assigning value by multiplying 10 to a
 // using "*=" operator a *= 10;
 printf("Value of a is %d\n", a);
 // Assigning value by dividing 10 from a
 // using "/=" operator a /= 10;
 printf("Value of a is %d\n", a);
 return 0; }
Output:Value of a is 10
Value of a is 20
Value of a is 10
Value of a is 100
Value of a is 10
#include <stdio.h>
int main()
{
int a = 10;
 int b = 20;
a += b;
printf("a: %d", a);
return 0;
}
a: 30
Shorthand Assignment operators
Simple assignment
Shorthand operator
operator
a = a+1 a + =1
a = a-1 a - =1
a = a* (m+n) a * = m+n
a = a / (m+n) a / = m+n
a = a %b a %=b
For example, the expression x = 5 assigns the
value of 5 to the variable x.
For example, the equation x += 5 is
equivalent to x = x + 5. It updates the value
of x by adding 5 to its current value.
x = 5; // assigns the value of 5 to the variable
x
y += 10; // adds 10 to the current value of y
and updates y with the result
z *= 3; // multiplies the current value of z by
3 and updates z with the result
 #include <stdio.h>
 int main() {
 int x = 5;
 int y = 10;
 printf("The initial values of x and y are: %d, %d\n", x, y);
 // Using the addition assignment operator
 x += y;
 printf("The updated value of x is: %d", x);
}
 The initial values of x and y are: 5, 10
The updated value of x is: 15
 Shorthand Addition Assignment (+=)
 #include <stdio.h>
 int main()
 {
 int a = 5;
 // Assign 5 to 'a'
 a += 1; // Same as a = a + 1
 printf("a = %d\n", a);
 return 0; }
// Output: a = 6
Shorthand Subtraction Assignment (-=)
 #include <stdio.h>
 int main()
 { int a = 5; // Assign 5 to 'a'
 a -= 2; // Same as a = a - 2
 printf("a = %d\n", a);
 return 0; }
 Output:a = 3
 Shorthand Multiplication Assignment (*=)
 #include <stdio.h>
 int main() {
 int a = 5; // Assign 5 to 'a'
 a *= 3; // Same as a = a * 3
 printf("a = %d\n", a);
 return 0;
 }
 Output:a = 15
 Shorthand Division Assignment (/=)
 #include <stdio.h>
 int main()
 { int a = 10; // Assign 10 to 'a'
 a /= 2; // Same as a = a / 2
 printf("a = %d\n", a);
 return 0; }
 // Output: a = 5
Shorthand Modulus Assignment (%=)
 #include <stdio.h>
 int main() {
 int a = 10; // Assign 10 to 'a'
 a %= 3; // Same as a = a % 3
 printf("a = %d\n", a);
 return 0; }
 // Output: a = 1
Increment & Decrement Operators
C supports 2 useful operators namely
1. Increment ++
2. Decrement – operators
The ++ operator adds a value 1 to the operand
The – operator subtracts 1 from the operand
++a or a++
--a or a--
Rules for ++ & -- operators
1. These require variables as their
operands
2. When postfix either ++ or – is used with
the variable in a given expression, the
expression is evaluated first and then it
is incremented or decremented by one
3. When prefix either ++ or – is used with
the variable in a given expression, it is
incremented or decremented by one first
and then the expression is evaluated
with the new value
Examples for ++ & -- operators
Let the value of a =5 and b=++a then
a = b =6
Let the value of a = 5 and b=a++ then
a =5 but b=6
i.e.:
1. a prefix operator first adds 1 to the
operand and then the result is assigned to
the variable on the left
2. a postfix operator first assigns the value
to the variable on left and then increments
the operand.
 // C Program to illustrate the increment of both type
 #include <stdio.h>
 void increment()
{
 int a = 5;
 int b = 5;
 // PREFIX
 int prefix = ++a;
 printf("Prefix Increment: %d\n", prefix);
 // POSTFIX
 int postfix = b++;
 printf("Postfix Increment: %d", postfix);
}
 // Driver code
 int main()
{
 increment();
 return 0;
 }
Output
Prefix Increment: 6
Postfix Increment: 5
Conditional operators
Syntax:
exp1 ? exp2 : exp3
Where exp1,exp2 and exp3 are expressions
Working of the ? Operator:
Exp1 is evaluated first, if it is nonzero(1/true) then the
expression2 is evaluated and this becomes the value of the
expression,
If exp1 is false(0/zero) exp3 is evaluated and its value
becomes the value of the expression
Ex: m=2;
n=3
r=(m>n) ? m : n;
The conditional operator in C is kind of similar
to the if-else statement as it follows the same
algorithm as of if-else statement but the
conditional operator takes less space and helps to
write the if-else statements in the shortest way
possible. It is also known as the
ternary operator Syntax of
Conditional/Ternary Operator in C
The conditional operator can be in the form
variable = Expression1 ? Expression2 :
Expression3;in C as it operates on three
operands.
It can be visualized into an if-else statement
as:
if(Expression1)
{
variable = Expression2;
}
else
{
variable = Expression3;
}
Example 1: C Program to Store the
greatest of the two Numbers using the
ternary operator C
// C program to find largest among two //
numbers using ternary operator
#include <stdio.h>
int main()
{ int m = 5, n = 4;
 (m > n) ? printf("m is greater than n that is
%d > %d", m, n) : printf("n is greater than m
that is %d > %d", n, m);
return 0;
}
Output:
m is greater than n that is 5 > 4
Example 2: C Program to check whether
a year is a leap year using ternary
operator C
// C program to check whether a year is leap year
or not // using ternary operator
 #include <stdio.h>
int main() {
int yr = 1900;
 (yr%4==0) ? (yr%100!=0? printf("The year %d
is a leap year",yr) : (yr%400==0 ? printf("The
year %d is a leap year",yr) :printf("The year %d is
not a leap year",yr))) : printf("The year %d is not
a leap year",yr);
return 0;
}
Bitwise operators
These operators allow manipulation of data at the bit level

Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< Shift left
>> Shift right
X Y X&Y X|Y X^Y

0 0 0 0 0

0 1 0 1 1

1 0 0 1 1

1 1 1 1 0
 explain every function// C Program to demonstrate use of bitwise
operators
 #include <stdio.h>
 int main() {
 // a = 5 (00000101 in 8-bit binary), b = 9 (00001001 in // 8-bit
binary) unsigned int a = 5, b = 9;
 // The result is 00000001
 printf("a = %u, b = %u\n", a, b);
 printf("a&b = %u\n", a & b);
 // The result is 00001101
 printf("a|b = %u\n", a | b);
 // The result is 00001100
 printf("a^b = %u\n", a ^ b);
 // The result is 11111111111111111111111111111010
 // (assuming 32-bit unsigned int)
 printf("~a = %u\n", a = ~a);
 // The result is 00010010
 printf("b<<1 = %u\n", b << 1);
 // The result is 00000100
 printf("b>>1 = %u\n", b >> 1);
 return 0; }
 The program uses bitwise operators on two unsigned
integers a and b. Here's a breakdown of the program:
 Includes and Main Function:
 #include <stdio.h> int main() {
 This includes the standard input-output library to use printf.
 The main function is the entry point of the program.
 Variable Declaration:
 unsigned int a = 5, b = 9;
 Two unsigned integers a and b are initialized to 5 and 9,
respectively.
 Bitwise Operations
 Now, let's look at each bitwise operation used in the program:
 AND Operator (&):
 printf("a&b = %u\n", a & b);
 Operation: a & b
 Binary Representation:
 a = 5 = 00000101
 b = 9 = 00001001
Result:00000101
 AND each bit: 0&0=0, 0&0=0, 0&1=0, 0&1=0, 0&0=0, 1&0=0,
0&1=0, 1&1=1
 Result = 00000001 (1 in decimal).
 OR Operator (|):
 printf("a|b = %u\n", a | b);
 Operation: a | b
 Result:
 00000101
 00001001
 OR each bit: 0|0=0, 0|0=0, 0|1=1, 0|1=1, 0|0=0, 1|0=1, 0|1=1, 1|
1=1
 Result = 00001101 (13 in decimal).
 XOR Operator (^):
 printf("a^b = %u\n", a ^ b);
 Operation: a ^ b
 Result:
 00000101
 00001001
 XOR each bit: 0^0=0, 0^0=0, 0^1=1, 0^1=1, 0^0=0, 1^0=1,
NOT Operator (~):
printf("~a = %u\n", a = ~a);
Operation: ~a
Result:
 a (initially 5) = 00000101
 NOT operation flips all bits: ~00000101 = 11111010 (in
binary).
 Since a is unsigned, this represents 4294967290 (for a
32-bit unsigned int) as a result.
 Left Shift Operator (<<):
c
 Copy code
 printf("b<<1 = %u\n", b << 1);
 Operation: b << 1
 Result:
 Shifts the bits of b (9 or 00001001) to the left by 1 position:
00010010 (18 in decimal).
 This effectively multiplies b by 2.
 Right Shift Operator (>>):
c
 Copy code
 printf("b>>1 = %u\n", b >> 1);
 Operation: b >> 1
 Result:
 Shifts the bits of b (9 or 00001001) to the right by 1 position:
00000100 (4 in decimal).
 This effectively divides b by 2 (discarding the remainder).
Special operators
1. Comma operator ( ,)
2. sizeof operator – sizeof( )
3. Pointer operators – ( & and *)
4. In the C programming language, special
operators are used to perform specific
operations that cannot be done with
normal arithmetic or logical operators. These
operators are special because they have their
own unique syntax and functionality. In this
blog post, we will explore some of the most
used special operators in C, including
the ternary, bitwise, and comma
operators.mber selection operators – ( . and -
>)
EXAMPLE PROGRAM FOR & AND * OPERATORS
IN C:
int main()
{
int *ptr, q;
q = 50;
/* address of q is assigned to ptr */
ptr = &q;
/* display q's value using ptr variable */
printf("%d", *ptr);
return 0;
}
50
Output: 50
EXAMPLE PROGRAM FOR SIZEOF() OPERATOR IN C:
 #include <limits.h>

 int main()
{
 int a;
 char b;
 float c;
 double d;
 printf("Storage size for int data type:%d \n",sizeof(a));
 printf("Storage size for char data type:%d \n",sizeof(b));
 printf("Storage size for float data type:%d \n",sizeof(c));
 printf("Storage size for double data type:%d\n",sizeof(d));
 return 0;
}
OUTPUT:

Storage size for int data type:4


Storage size for char data type:1
Storage size for float data type:4
Storage size for double data type:8
Syntax:
The syntax of the ternary operator is as follows:
condition ?value_if_true : value_if_false;
#include <stdio.h>
 int main() {
 int num = 7;
printf("%d is %s\
n", num, num % 2 == 0 ? "even" : "odd");
 return 0;
}
Output
7 is odd

Arithmetic Expressions
Algebraic expression C expression
axb-c a*b-c
(m+n)(x+y) (m+n)*(x+y)
ab a*b/c
c

3x2+2x+1 3*x*x+2*x+1
a a/b
b

a b c S=(a+b+c)/2
S=
2
Arithmetic Expressions
Algebraic expression C expression
area= s( s  a)( s  b)( s  c) area=sqrt(s*(s-a)*(s-b)*(s-c))

Sin 
 b 

sin(b/sqrt(a*a+b*b))
 2 2 
 a b 

 x   y 
1     xy
2
tow1=sqrt((rowx-rowy)/2+tow*x*y*y)
 2 

   y 
2
tow1=sqrt(pow((rowx-rowy)/2,2)+tow*x*y*y)
1   x   xy
2

 2 

y=(alpha+beta)/sin(theta*3.1416/180)+abs(x)
Precedence of operators
BODMAS RULE-
Brackets of Division Multiplication Addition
Subtraction
Brackets will have the highest precedence and
have to be
evaluated first, then comes of , then comes
division, multiplication, addition and finally
subtraction.
C language uses some rules in evaluating the
expressions
and they r called as precedence rules or sometimes
also
referred to as hierarchy of operations, with some
operators
// C Program to illustrate operator
precedence #include <stdio.h>
 int main()
{
// printing the value of same expression
printf("10 + 20 * 30 = %d", 10 + 20 * 30);
return 0;
}
Output
10 + 20 * 30 = 610
// C Program to illustrate operator
Associativity #include <stdio.h>
 int main()
{
// Verifying the result of the same expression
printf("100 / 5 % 2 = %d", 100 / 5 % 2);
 return 0;
}
Output:100 / 5 % 2 = 0
Let’s evaluate the following expression,
100 / 5 % 2
According to the given table, the associativity
of the multiplicative operators is from Left to
Right. So,
(100 / 5) % 2
After evaluation, the expression will be
20 % 2
Now, the % operator will be evaluated.
0
Rules for evaluation of expression
1. First parenthesized sub expression from left to right are
evaluated.
2. If parentheses are nested, the evaluation begins with the
innermost sub expression
3. The precedence rule is applied in determining the order
of application of operators in evaluating sub expressions
4. The associatively rule is applied when 2 or more
operators of the same precedence level appear in a sub
expression.
5. Arithmetic expressions are evaluated from left to right
using the rules of precedence
6. When parentheses are used, the expressions within
parentheses assume highest priority
Hierarchy of operators

Operator Description Associativity


( ), [ ] Function call, array Left to Right
element reference
+, -, ++, - Unary plus, minus,
-,!,~,*,& increment, decrement,
logical negation, 1’s Right to Left
complement, pointer
reference, address
*, / , % Multiplication, Left to Right
division, modulus
Example 1
Evaluate x1=(-b+ sqrt (b*b-4*a*c))/(2*a) @ a=1,
b=-5, c=6
=(-(-5)+sqrt((-5)(-5)-4*1*6))/(2*1)
=(5 + sqrt((-5)(-5)-4*1*6))/(2*1)
=(5 + sqrt(25 -4*1*6))/(2*1)
=(5 + sqrt(25 -4*6))/(2*1)
=(5 + sqrt(25 -24))/(2*1)
=(5 + sqrt(1))/(2*1)
=(5 + 1.0)/(2*1)
=(6.0)/(2*1)
=6.0/2 = 3.0
Example 2
Evaluate the expression when a=4
b=a- ++a
=a – 5
=5-5
=0

You might also like