unit 2 fpl
unit 2 fpl
2.1 Operators
Q. What is operator? Enlist and explain types of operators in C.
Ans.
In C language, operators are symbols that represent operations to be performed on one
or more operands. They are the basic components of the C programming.
C supports a rich set of built-in operators. We have already used several of them, such as
=, +, –, *, & and <.
An operator is a symbol that tells the computer to perform certain mathematical or
logical manipulations. Operators are used in programs to manipulate data and variables.
They usually form a part of the mathematical or logical expressions.
Operators are fundamental for manipulating data, making decisions, and controlling
program flow. They allow programmers to perform computations, compare values, and
manage memory efficiently.
The values and variables used with operators are called operands. So we can say that
the operators are the symbols that perform operations on operands.
Types of 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.
1
FPL Unit-II DIT,Pimpri
1. Arithmetic Operators
Arithmetic operators in C are used to perform mathematical operations on variables and
constants.
C provides all the basic arithmetic operators. The operators +, –, *, and / all work the
same way as they do in other languages. These can operate on any built-in data type
allowed in C. The unary minus operator, in effect, multiplies its single operand by –1.
Therefore, a number preceded by a minus sign changes its sign.
Example:
Sr.No. Symbol Meaning Example Output
1 + Addition 5+3 8
2 - Subtraction 5-3 2
3 * Multiplication 5*3 15
4 / Division 6/3 2
5 % Modulus 5%3 2
Program Example:
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("Addition: %d\n", a + b);
printf("Subtraction: %d\n", a - b);
printf("Multiplication: %d\n", a * b);
printf("Division: %d\n", a / b);
printf("Modulus: %d\n", a % b);
return 0;
}
Output:
Addition: 13
Subtraction: 7
Multiplication: 30
2
FPL Unit-II DIT,Pimpri
Division: 3
Modulus: 1
2. Relational Operators
We often compare two quantities and depending on their relation, take certain decisions.
For example, we may compare the age of two persons, or the price of two items, and so
on. These comparisons can be done with the help of relational operators. We have
already used the symbol ‘<‘, meaning ‘less than’. An expression such as
a < b or 1 < 20
10 < 20 is true
but
20 < 10 is false
Program Example:
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("a == b: %d\n", a == b); // 0 (false)
printf("a != b: %d\n", a != b); // 1 (true)
printf("a > b: %d\n", a > b); // 1 (true)
3
FPL Unit-II DIT,Pimpri
3. Logical Operators
Logical operators are used to combine two or more conditions.
The logical operators && and || are used when we want to test more than one condition
and make decision. An example
a > b && x == 10.
An expression of this kind, which combines two or more relational expressions,
is termed as a logical expression or a compound relational expression. Like the simple
relational expressions, a logical expression also yields a value of one or zero. The logical
expression given above is true only if a > b is true and x == 10 is true. If either (or
both) of them are false, the expression is false.
Program Example:
#include <stdio.h>
int main() {
int a = 1, b = 0;
printf("a && b: %d\n", a && b);
printf("a || b: %d\n", a || b);
printf("!a: %d\n", !a);
return 0;
}
4
FPL Unit-II DIT,Pimpri
Output:
a && b: 0
a || b: 1
!a: 0
4. Assignment Operators
Program Example:
#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing results
printf("a = b: %d\n", a = b);
printf("a += b: %d\n", a += b);
printf("a -= b: %d\n", a -= b);
printf("a *= b: %d\n", a *= b);
printf("a /= b: %d\n", a /= b);
printf("a %= b: %d\n", a %= b);
printf("a &= b: %d\n", a &= b);
printf("a |= b: %d\n)", a |= b);
printf("a >>= b: %d\n", a >> b);
printf("a <<= b: %d\n", a << b);
return 0;
}
Output:
a = b: 5
a += b: 10
a -= b: 5
a *= b: 25
a /= b: 5
a %= b: 0
a &= b: 0
a |= b: 5
a >>= b: 0
a <<= b: 160
6
FPL Unit-II DIT,Pimpri
We use the increment and decrement statements in for and while loops extensively.
1. Increment and decrement operators are unary operators and they require variable
as their operands.
2. When postfix ++ (or --) is used with variable in an expression, the expression is
evaluated first using the original value of the variable and then the variable is
incremented (or decremented) by one.
3. When prefix ++ (or --) is used in an expression, the variable is incremented (or
decremented) first then the expression is evaluated using the new value of the variable.
4. The precedence and associatively of ++ and – – operators are the same as those of unary
+ and unary –.
Program Example:
#include <stdio.h>
int main() {
int a = 10;
printf("%d\n",a++); // a = a + 1 => a = 11
printf("%d\n",a--); // a = a - 1 => a = 10
int count = 5;
printf("%d\n", ++count); // prints 6 (prefix increment)
printf("%d\n", count--); // prints 6, then count becomes 5 (postfix decrement)
return 0;
}
Output:
10
11
6
6
7
FPL Unit-II DIT,Pimpri
6. Conditional Operators
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 in
C as it operates on three operands.
Program Example:
#include <stdio.h>
int main() {
int a = 10, b = 5;
int max = (a > b) ? a : b;
printf("Maximum value is %d\n", max);
return 0;
}
Output:
Maximum value is 10
7. Bitwise Operators
C has a distinction of supporting special operators known as bitwise operators for
manipulation of data at bit level. These operators are used for testing the bits, or shifting
them right or left. Bitwise operators may not be applied to float or double.
Here are the bitwise operators:
8
FPL Unit-II DIT,Pimpri
Program Example:
#include <stdio.h>
int main() {
int a = 5, b = 3;
printf("a & b: %d\n", a & b); // 1
printf("a | b: %d\n", a | b); // 7
printf("a ^ b: %d\n", a ^ b); // 6
printf("~a: %d\n", ~a); // -6
printf("a << 2: %d\n", a << 2); // 20
printf("a >> 2: %d\n", a >> 2); // 1
return 0;
}
Output:
a & b: 1
a | b: 7
a ^ b: 6
~a: -6
a << 2: 20
a >> 2: 1
8. Special Operators
Special operators include sizeof, comma operator, pointer operators, etc.
The comma operator can be used to link the related expressions together. A
comma-linked list of expressions are evaluated left to right and the value of right-most
expression is the value of the combined expression. For example, the statement
value = (x = 10, y = 5, x+y);
first assigns the value 10 to x, then assigns 5 to y value, and finally assigns 15(i.e 10+5)
to value. Since comma operator has the lowest precedence of all operators, the
parentheses are necessary. Some applications of comma operator are:
In for loop:
for ( n = 1, m = 10, n <=m; n++, m++)
In while loop:
while (c = getchar( ), c != ‘10’)
9
FPL Unit-II DIT,Pimpri
exchanging values:
t = x, x = y, y = t;
The sizeof is a compile time operator and, when used with an operand, it returns the
number of bytes the operand occupies. The operand may be a variable, a constant or a
data type qualifier.
Examples: m= sizeof (sum);
n = sizeof (long int);
k = sizeof (235L);
The sizeof operator is normally used to determine the lengths of arrays and structures
when their sizes are not known to the programmer. It is also used to allocate memory
space dynamically to variables during execution of a program.
Program Example:
#include <stdio.h>
int main() {
int a = 10;
printf("Size of a: %lu\n", sizeof(a)); // 4 (bytes)
int b = (a, 20); // b = 20
printf("%d\n",b);
int *p = &a; // p stores the address of a
printf("Value at address p: %d\n", *p); // 10
return 0;
}
Output:
Size of a: 4
20
Value at address p: 10
Here’s a breakdown:
10
FPL Unit-II DIT,Pimpri
Variables: Represent values that can change during the execution of a program.
Constants: Fixed values that do not change during program execution.
Operators: Symbols like +, -, *, /, % that perform arithmetic operations (addition, subtraction,
multiplication, division, modulus) on variables and constants.
Program Example:
#include <stdio.h>
int main() {
int a = 10, b = 3, c;
c = a + b * 2; // c = 10 + 3*2 = 10 + 6 = 16
printf("%d\n",c);
return 0;
Output:
16
Ans.
Evaluation of Expressions:
variable=expression;
11
FPL Unit-II DIT,Pimpri
Variable is any valid C variable name. when the statement is encountered, the expression
is evaluated first and then the replaces the previous value of a variable on the left hand
side. All variables used in expression must be assigned values before evaluation is
attempted.
x = a * b - c;
y = b / c * a;
z = a – b / c + d;
The blank space around an operator is optional and adds only to improve readability.
When these statements are used in program, the variable a, b, c and d must be defined
before they used in the expressions.
Example:
#include <stdio.h>
int main() {
float a,b,c,x,y,z;
a=9;
b=12;
c=3;
x=a-b/3+c*2-1;
y=a-b/(3+c)*(2-1);
z=a-(b/(3+c)*2)-1;
printf("x= %f\n",x);
printf("y= %f\n",y);
printf("z= %f\n",z);
Output:
x= 10.000000
12
FPL Unit-II DIT,Pimpri
y= 7.000000
z= 4.000000
Types of expression
Infix Expressions
Infix expressions are the most common form of expressions in C and other programming languages. In an
infix expression, the operator is placed between the operands.
Example:
Prefix Expressions
Prefix expressions, also known as Polish notation, have the operator placed before the operands. While C
doesn't natively support prefix expressions in the way it does infix, you can evaluate prefix expressions
using data structures like stacks.
Example of Prefix Expression: + 3 4
To evaluate prefix expressions in C, you can write a program using a stack to convert and evaluate them.
Postfix Expressions
Postfix expressions, also known as Reverse Polish notation (RPN), have the operator placed after the
operands. Similar to prefix expressions, C doesn't natively support postfix expressions but you can
evaluate them using stacks.
Example of Postfix Expression: 3 4 +
x = 9-12/3 + 3*2-1
13
FPL Unit-II DIT,Pimpri
Program Example:
#include <stdio.h>
int main() {
printf("%d\n",result);
return 0;
}
14
FPL Unit-II DIT,Pimpri
Output:
20
10 + 20 * 30
The expression contains two operators, + (plus), and * (multiply). According to the given table,
the * has higher precedence than + so, the first evaluation will be
10 + (20 * 30)
10 + 600
610
Operator associativity is used when two operators of the same precedence appear in an
expression. Associativity can be either from Left to Right or Right to Left.
Example
Let’s evaluate the following expression,
100 / 5 % 2
Both / (division) and % (Modulus) operators have the same precedence, so the order of
evaluation will be decided by associativity.
According to the given table, the associativity of the multiplicative operators is from Left to
Right. So,
(100 / 5) % 2
20 % 2
15
FPL Unit-II DIT,Pimpri
Description Associativity
Operator
Precedence
16
FPL Unit-II DIT,Pimpri
Description Associativity
Operator
Precedence
equal to
12 || Logical OR Left-to-Right
= Assignment
14 Right-to-Left
%= , &= Modulus, bitwise AND assignment
17
FPL Unit-II DIT,Pimpri
Mathematical functions such as cos, sqrt, log, etc. are frequently used in analysis of real-
life problems.
Most of the C compilers support these basic math functions. However, there are systems
that have functions are available. Table 3.9 lists some standard math functions.
Mathematical functions in C are provided by the <math.h> library, which offers a variety
of functions to perform mathematical operations.
The math.h library provides various mathematical functions like sqrt, pow, sin, etc.
18
FPL Unit-II DIT,Pimpri
Question bank
1. What are the different arithmetic operators in C? Provide examples for each.
2. How does integer division differ from floating-point division in C.
3. List all relational operators in C and give an example of each.
4. Write a C program to compare two integers and display whether they are equal or not.
5. How do relational operators interact with if-else statements in C?
6. What are the logical operators available in C? Provide examples.
7. Explain the difference between the logical AND (&&) and logical OR (||) operators with
examples. What is the result of the expression (5 > 3) && (3 < 2)? Explain.
8. List different assignment operators in C. Explain the difference between = and == in C
9. How do compound assignment operators (e.g., +=, -=) work in C? Provide examples.
10. Write a C program demonstrating the use of assignment operators.
11. What are the increment and decrement operators in C? Provide examples.
12. What will be the output of the following code snippet?
#include <stdio.h>
int main() {
int x = 5;
printf("%d\n", x++);
printf("%d\n", ++x);
return 0;
}
13. What is the conditional (ternary) operator in C? Provide the syntax and an example.
14. Write a C program using the conditional operator to find the largest of two numbers.
15. List all the bitwise operators in C and give an example of each.
16. How does the bitwise shift operator (<<, >>) work? Provide examples.
17. What are the special operators in C? Explain the purpose of the sizeof operator with an
example.
18. What is the purpose of the comma operator? Provide an example.
19. Write a C program demonstrating the use of the sizeof operator on different data types.
20. What are arithmetic expressions in C? Provide examples.
21. Explain the order of evaluation of the expression a + b * c - d / e.
22. What is operator precedence? Why is it important in C?
23. List the precedence of arithmetic operators in C from highest to lowest.
24. How does precedence affect the evaluation of the expression a + b * c?
25. Explain the difference between operator precedence and associativity. What is the
associativity of the arithmetic operators in C?
26. What are some common mathematical functions available in C? Provide examples.
27. How do you include and use the math library in a C program? Explain the use of the sqrt
and pow functions with examples.
19
FPL Unit-II DIT,Pimpri
Practice Programs
20