0% found this document useful (0 votes)
16 views

Unit II FPL Notes

Uploaded by

ihavecrush55
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)
16 views

Unit II FPL Notes

Uploaded by

ihavecrush55
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/ 27

Unit II

Operators and Expressions


(06 Hours)
Arithmetic Operators, Relational Operators, Logical Operators, Assignment
Operators, Increment and Decrement Operators, Conditional Operators,
Bitwise Operators, Special Operators. Arithmetic Expressions, Evaluation of
Expressions, Precedence of Arithmetic Operators, Operator Precedence and
Associativity, Mathematical Functions.

Operator:
An operator is a symbol that operates on a value or a variable. For example: + is an operator
to perform addition.
C has a wide range of operators to perform various operations.

➢ C Arithmetic Operators:

Operator Meaning of Operator

+ addition or unary plus

- subtraction or unary minus

* Multiplication

/ Division

% remainder after division (modulo division)

An arithmetic operator performs mathematical operations such as addition, subtraction,


multiplication, division etc on numerical values (constants and variables).
Example 1: Arithmetic Operators
// Working of arithmetic operators
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);

return 0;
}

Output
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
The operators +, - and * computes addition, subtraction, and multiplication respectively as
you might have expected.
In normal calculation, 9/4 = 2.25. However, the output is 2 in the program.
It is because both the variables a and b are integers. Hence, the output is also an integer. The
compiler neglects the term after the decimal point and shows answer 2 instead of 2.25.
The modulo operator % computes the remainder. When a=9 is divided by b=4, the
remainder is 1. The % operator can only be used with integers.
Suppose a = 5.0, b = 2.0, c = 5 and d = 2. Then in C programming,
// Either one of the operands is a floating-point number
a/b = 2.5
a/d = 2.5
c/b = 2.5
// Both operands are integers
c/d = 2

➢ C Increment and Decrement Operators


C programming has two operators increment ++ and decrement -- to change the value of an
operand (constant or variable) by 1.
Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These
two operators are unary operators, meaning they only operate on a single operand.
Example 2: Increment and Decrement Operators
// Working of increment and decrement operators
#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
return 0;
}
Output
++a = 11
--b = 99
++c = 11.500000
--d = 99.500000
Here, the operators ++ and -- are used as prefixes. These two operators can also be used as
postfixes like a++ and a--.

➢ C Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common
assignment operator is =

Operator Example Same as

= a=b a=b

+= a += b a = a+b

-= a -= b a = a-b

*= a *= b a = a*b

/= a /= b a = a/b

%= a %= b a = a%b

Example 3: Assignment Operators // Working of assignment operators


#include <stdio.h>
int main()
{
int a = 5, c;
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);
return 0;
}
Output
c=5
c = 10
c=5
c = 25
c=5
c=0

➢ C Relational Operators:

Operator Meaning of Operator Example

== Equal to 5 == 3 is evaluated to 0

> Greater than 5 > 3 is evaluated to 1

< Less than 5 < 3 is evaluated to 0

!= Not equal to 5 != 3 is evaluated to 1


Operator Meaning of Operator Example

>= Greater than or equal to 5 >= 3 is evaluated to 1

<= Less than or equal to 5 <= 3 is evaluated to 0

A relational operator checks the relationship between two operands. If the relation is true, it
returns 1; if the relation is false, it returns value 0.
Relational operators are used in decision making and loops.

Example 4: Relational Operators


// 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;
}

Output:
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 Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether
expression results true or false. Logical operators are commonly used in decision making in C
programming

Operator Meaning Example

Logical AND. True


If c = 5 and d = 2 then, expression ((c==5) &&
&& only if all operands
(d>5)) equals to 0.
are true
Operator Meaning Example

Logical OR. True only


If c = 5 and d = 2 then, expression ((c==5) ||
|| if either one operand
(d>5)) equals to 1.
is true

Logical NOT. True


! only if the operand is If c = 5 then, expression !(c==5) equals to 0.
0

Example 5: Logical Operators


// Working of logical operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;

result = (a == b) && (c > b);


printf("(a == b) && (c > b) is %d \n", result);

result = (a == b) && (c < b);


printf("(a == b) && (c < b) is %d \n", result);

result = (a == b) || (c < b);


printf("(a == b) || (c < b) is %d \n", result);

result = (a != b) || (c < b);


printf("(a != b) || (c < b) is %d \n", result);
result = !(a != b);
printf("!(a != b) is %d \n", result);

result = !(a == b);


printf("!(a == b) is %d \n", result);

return 0;
}
Output:
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
Explanation of logical operator program
• (a == b) && (c > 5) evaluates to 1 because both operands (a == b) and (c > b) is 1
(true).
• (a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).
• (a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true).
• (a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c < b) are 0
(false).
• !(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a != b) is 1
(true).
• !(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0 (false).

➢ C Bitwise Operators:
During computation, mathematical operations like: addition, subtraction, multiplication,
division, etc are converted to bit-level which makes processing faster and saves power.
Bitwise operators are used in C programming to perform bit-level operations.
Operators Meaning of operators

& Bitwise AND

| Bitwise OR

^ Bitwise exclusive OR

~ Bitwise complement

<< Shift left

>> Shift right

Bitwise AND Operator &


The output of bitwise AND is 1 if the corresponding bits of two operands is 1. If either bit of
an operand is 0, the result of corresponding bit is evaluated to 0.
In C Programming, the bitwise AND operator is denoted by &.
Let us suppose the bitwise AND operation of two integers 12 and 25.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bit Operation of 12 and 25
00001100
& 00011001
________
00001000 = 8 (In decimal)
Example 1: Bitwise AND
#include <stdio.h>
int main() {
int a = 12, b = 25;
printf("Output = %d", a & b);
return 0;
}
Output
Output = 8

Bitwise OR Operator |
The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. In C
Programming, bitwise OR operator is denoted by |.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)

Bitwise OR Operation of 12 and 25


00001100
| 00011001
________
00011101 = 29 (In decimal)
Example 2: Bitwise OR
#include <stdio.h>
int main() {
int a = 12, b = 25;
printf("Output = %d", a | b);
return 0;
}

Output
Output = 29
Bitwise XOR (exclusive OR) Operator ^
The result of bitwise XOR operator is 1 if the corresponding bits of two operands are
opposite. It is denoted by ^.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bitwise XOR Operation of 12 and 25
00001100
^ 00011001
________
00010101 = 21 (In decimal)

Example 3: Bitwise XOR


#include <stdio.h>
int main() {
int a = 12, b = 25;
printf("Output = %d", a ^ b);
return 0;
}
Output
Output = 21

Bitwise Complement Operator ~


Bitwise complement operator is a unary operator (works on only one operand). It
changes 1 to 0 and 0 to 1. It is denoted by ~.
35 = 00100011 (In Binary)
Bitwise complement Operation of 35
~ 00100011
________
11011100 = 220 (In decimal)
Twist in Bitwise Complement Operator in C Programming
The bitwise complement of 35 (~35) is -36 instead of 220, but why?
For any integer n, bitwise complement of n will be –(n + 1). To understand this, you should
have the knowledge of 2’s complement.
2’s Complement
Two’s complement is an operation on binary numbers. The 2’s complement of a number is
equal to the complement of that number plus 1. For example:
Decimal Binary 2’s complement
0 00000000 -(11111111+1) = -00000000 = -0(decimal)
1 00000001 -(11111110+1) = -11111111 = -256(decimal)
12 00001100 -(11110011+1) = -11110100 = -244(decimal)
220 11011100 -(00100011+1) = -00100100 = -36(decimal)

Note: Overflow is ignored while computing 2’s complement.


The bitwise complement of 35 is 220 (in decimal). The 2’s complement of 220 is -36. Hence,
the output is -36 instead of 220.
Bitwise Complement of Any Number N is –(N+1). Here’s how:
bitwise complement of N = ~N (represented in 2’s complement form)
2’complement of ~N= -(~(~N)+1) = -(N+1)

Example 4: Bitwise complement


#include <stdio.h>
int main() {
printf("Output = %d\n", ~35);
printf("Output = %d\n", ~-12);
return 0;
}
Output
Output = -36
Output = 11
Shift Operators in C programming
There are two shift operators in C programming:
• Right shift operator
• Left shift operator.
Right Shift Operator
Right shift operator shifts all bits towards right by certain number of specified bits. It is
denoted by >>.
212 = 11010100 (In binary)
212 >> 2 = 00110101 (In binary) [Right shift by two bits]
212 >> 7 = 00000001 (In binary)
212 >> 8 = 00000000
212 >> 0 = 11010100 (No Shift)

Left Shift Operator


Left shift operator shifts all bits towards left by a certain number of specified bits. The bit
positions that have been vacated by the left shift operator are filled with 0. The symbol of
the left shift operator is <<.
212 = 11010100 (In binary)
212<<1 = 110101000 (In binary) [Left shift by one bit]
212<<0 = 11010100 (Shift by 0)
212<<4 = 110101000000 (In binary) =3392(In decimal)

Example #5: Shift Operators


#include <stdio.h>
int main() {
int num=212, i;
for (i = 0; i <= 2; ++i) {
printf("Right shift by %d: %d\n", i, num >> i);
}
printf("\n");
for (i = 0; i <= 2; ++i) {
printf("Left shift by %d: %d\n", i, num << i);
}
return 0;
}
Output:
Right Shift by 0: 212
Right Shift by 1: 106
Right Shift by 2: 53

Left Shift by 0: 212


Left Shift by 1: 424
Left Shift by 2: 848

➢ Other Operators:

• The sizeof operator


The sizeof is a unary operator that returns the size of data (constants, variables, array,
structure, etc).
Example 6: sizeof Operator
#include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
return 0;
}
Output
Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte
A single expression in C may have multiple operators of different types. The C compiler
evaluates its value based on the operator precedence and associativity of operators.
The precedence of operators determines the order in which they are evaluated in an
expression. Operators with higher precedence are evaluated first.
For example, take a look at this expression −
x = 7 + 3 * 2;
Here, the multiplication operator "*" has a higher precedence than the addition operator
"+". So, the multiplication 3*2 is performed first and then adds into 7, resulting in "x = 13".
The following table lists the order of precedence of operators in C. Here, operators with the
highest precedence appear at the top of the table, and those with the lowest appear at the
bottom.

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

Within an expression, higher precedence operators will be evaluated first.

Operator Associativity:
In C, the associativity of operators refers to the direction (left to right or right to left) an
expression is evaluated within a program. Operator associativity is used when two operators
of the same precedence appear in an expression.
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
Example 1:
In the following code, the multiplication and division operators have higher precedence than
the addition operator.
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;
}
Output:
When you run this code, it will produce the following output −
e: 50
Example 2
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 this expression, the addition of a and b in parenthesis is first. The result is multiplied by c
and then the division by d takes place.
Example 3
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;
}
Output:
On running this code, you will get the following output −
e: 90

➢ Precedence of Post / Prefix Increment / Decrement Operators :

The "++" and "− −" operators act as increment and decrement operators, respectively. They
are unary in nature and can be used as a prefix or postfix to a variable.
When used as a standalone, using these operators in a prefix or post−fix manner has the
same effect. In other words, "a++" has the same effect as "++a". However, when these "++"
or "− −" operators appear along with other operators in an expression, they behave
differently.
Postfix increment and decrement operators have higher precedence than prefix increment
and decrement operators.
Example
The following example shows how you can use the increment and decrement operators in a
C program −
#include <stdio.h>
int main(){
int x = 5, y = 5, z;
printf("x: %d \n", x);
z = x++;
printf("Postfix increment: x: %d z: %d\n", x, z);
z = ++y;
printf("Prefix increment. y: %d z: %d\n", y ,z);
return 0;
}
Output:
x: 5
Postfix increment: x: 6 z: 5
Prefix increment. y: 6 z: 6
Logical operators have left−to−right associativity. However, the compiler evaluates the least
number of operands needed to determine the result of the expression. As a result, some
operands of the expression may not be evaluated.
For example, take a look at the following expression −
x > 50 && y > 50
Here the second operand "y > 50" is evaluated only if the first expression evaluates to True.

➢ Mathematical Functions in C:
C language provides various functions to perform mathematical operations
on numbers such as finding trigonometric ratios, calculating log and
exponentials, rounding the numbers, etc.. To use these math functions in a
C program, you need to include math.h header file.
Trigonometric Functions
The math.h library defines the function sin(), cos(), and tan() – that return the respective
trigonometric ratios, sine, cosine and tangent of an angle.
These functions return the respective ratio for a given double type representing the angle
expressed in radians. All the functions return a double value.
double sin(double x)
double cos(double x)
double tan(double x)
For all the above functions, the argument "x" is the angle in radians.
Example
The following example shows how you can use trigonometric functions in C −
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
int main() {
double x, sn, cs, tn, val;
x = 45.0;
val = PI / 180;

sn = sin(x*val);
cs = cos(x*val);
tn = tan(x*val);

printf("sin(%f) : %f\n", x, sn);


printf("cos(%f) : %f\n", x, cs);
printf("tan(%f) : %f\n", x, tn);

return(0);
}
Output
When you run this code, it will produce the following output −
sin(45.000000) : 0.707107
cos(45.000000) : 0.707107
tan(45.000000) : 1.000000

Power and Square Root Functions


The pow() and sqrt() functions are used to calculate the power and square root of the given
number.

pow() Function
This function returns x raised to the power of y i.e. xy.

double pow(double x, double y)


sqrt() Function
returns the square root of x.

double sqrt(double x)
The sqrt(x) function returns a value which is same as pow(x, 0.5)

Example
The following example shows how you can use the pow() and sqrt() functions in a C program

#include <stdio.h>
#include <math.h>

int main() {

double x = 9, y=2;
printf("Square root of %lf is %lf\n", x, sqrt(x));
printf("Square root of %lf is %lf\n", x, pow(x, 0.5) );
printf("%lf raised to power %lf\n", x, pow(x, y));

return(0);
}
Output
When you run this code, it will produce the following output −

Square root of 9.000000 is 3.000000


Square root of 9.000000 is 3.000000
9.000000 raised to power 81.000000
Rounding Functions
The math.h library includes ceil(), floor(), and round() functions that round off the given
floating point number.

ceil() Function
This returns the smallest integer value greater than or equal to x.

double ceil(double x)
This function returns the smallest integral value not less than x.

floor() Function
This function returns the largest integer value less than or equal to x.

double floor(double x)
Parameter x : This is the floating point value. This function returns the largest integral value
not greater than x.

round() Function
This function is used to round off the double, float or long double value passed to it as a
parameter to the nearest integral value.

double round( double x )


The value returned is the nearest integer represented as floating point

Example
The following example demonstrates how you can use the rounding functions in a C program

#include <stdio.h>
#include <math.h>

int main() {

float val1, val2, val3, val4;

val1 = 1.2;
val2 = 1.6;
val3 = 2.8;
val4 = -2.3;

printf ("ceil(%lf) = %.1lf\n", val1, ceil(val1));


printf ("floor(%lf) = %.1lf\n", val2, floor(val2));
printf ("ceil(%lf) = %.1lf\n", val3, ceil(val3));
printf ("floor(%lf) = %.1lf\n", val4, floor(val4));

printf("round(%lf) = %.1lf\n", val1, round(val1));


printf("round(%lf) = %.1lf", val4, round(val4));
return(0);
}
Output
When you run this code, it will produce the following output −

ceil(1.200000) = 2.0
floor(1.600000) = 1.0
ceil(2.800000) = 3.0
floor(-2.300000) = -3.0
round(1.200000) = 1.0
round(-2.300000) = -2.0
Modulus Functions
The "math.h" library includes the following functions in this category:

modf() Function
The modf() function returns the fraction component (part after the decimal), and sets
integer to the integer component.

double modf(double x, double *integer)


Here, "x" is the floating point value and "integer" is the pointer to an object where the
integral part is to be stored.

This function returns the fractional part of "x" with the same sign.

Example
Take a look at the following example −

Open Compiler
#include <stdio.h>
#include <math.h>
int main () {

double x, fractpart, intpart;

x = 8.123456;
fractpart = modf(x, &intpart);

printf("Integral part = %lf\n", intpart);


printf("Fraction Part = %lf \n", fractpart);

return(0);
}
Output
When you run this code, it will produce the following output −

Integral part = 8.000000


Fraction Part = 0.123456
fmod() Function
The fmod() function returns the remainder of x divided by y.

double fmod(double x, double y)


Here, "x" is the numerator and "y" is the denominator. The function returns remainder of "x
/ y".

Example
Take a look at the following example −

#include <stdio.h>
#include <math.h>
int main () {

float a, b;
int c;
a = 9.2;
b = 3.7;
c = 2;

printf("Remainder of %f / %d is %lf\n", a, c, fmod(a,c));


printf("Remainder of %f / %f is %lf\n", a, b, fmod(a,b));

return(0);
}
Output
When you run this code, it will produce the following output −

Remainder of 9.200000 / 2 is 1.200000


Remainder of 9.200000 / 3.700000 is 1.800000
Note that the modulus operator (%) works only with integer operands.

You might also like