FPL Unit 2 Notes
FPL Unit 2 Notes
FPL Unit-II
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. Arithmetic Operators
• Arithmetic operators in C are used to perform mathematical operations on variables and
constants.
FPL Unit-II
• 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:
[Link]. 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
2
FPL Unit-II
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
3
FPL Unit-II
}
Output:
a == b:
0 a !=
b: 1 a >
b: 1 a <
b: 0 a
>= b: 1
a <= b:
0
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.
Output:
a && b:
0
a || b: 1
4
FPL Unit-II
!a: 0
4. Assignment Operators
5
FPL Unit-II
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
We use the increment and decrement statements in for and while loops extensively.
6
FPL Unit-II
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
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.
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
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.
• sizeof: Returns the size of a variable or datatype. Comma ‘,’: Separates expressions.
• Pointer *: Value at address.
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
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
• 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:
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;
• 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.
11
FPL Unit-II
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/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
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 +
2.4 Precedence of Arithmetic Operators
Q. what is precedence of Arithmetic Operators? Explain Rules for Evaluation of Expression.
13
FPL Unit-II
x = 9-12/3 + 3*2-1
14
FPL Unit-II
Program Example:
#include <stdio.h>
int main() {
return 0;
}
Output:
15
FPL Unit-II
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
Description Associativity
Precedence Operator
. Dot Operator
* Dereference Operator
17
FPL Unit-II
18
FPL Unit-II
• Mathematical functions such as cos, sqrt, log, etc. are frequently used in analysis of
reallife 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.
20
FPL Unit-II
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.
21
FPL Unit-II
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.
Practice Programs
22