Unit II FPL Notes
Unit II FPL Notes
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:
* Multiplication
/ Division
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 Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common
assignment operator is =
= 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
➢ C Relational Operators:
== 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.
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
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 OR
^ Bitwise exclusive OR
~ Bitwise complement
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)
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)
➢ Other Operators:
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
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);
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
pow() Function
This function returns x raised to the power of y i.e. xy.
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 −
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.
Example
The following example demonstrates how you can use the rounding functions in a C program
−
#include <stdio.h>
#include <math.h>
int main() {
val1 = 1.2;
val2 = 1.6;
val3 = 2.8;
val4 = -2.3;
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.
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 () {
x = 8.123456;
fractpart = modf(x, &intpart);
return(0);
}
Output
When you run this code, it will produce the following output −
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;
return(0);
}
Output
When you run this code, it will produce the following output −