Unit 13 - V1
Unit 13 - V1
C PROGRAMMING
Unit 13
Functions Part 3
Table of Contents
1. INTRODUCTION
While C does not support native operator overloading like C++, functions can emulate
operator-like behavior for custom data types, providing similar functionality. Unary
operators act on a single operand, and functions can be used to implement custom behaviors
for unary operations. Bitwise operators manipulate individual bits of integer operands, and
functions can encapsulate bitwise operations to perform tasks such as setting, clearing, or
toggling specific bits. Logical operators perform logical operations on boolean values, and
functions can incorporate logical operators to make decisions and control program flow
based on boolean conditions.
1.1. Objectives:
However, you can simulate operator overloading in C by defining functions that perform the
desired operations and then calling those functions explicitly. For example, instead of
overloading the + operator for custom types, you can define a function like add() to perform
addition operations on objects of those types. In C programming, operator overloading is not
directly supported as it is in languages like C++. However, we can still demonstrate unary
operators, bitwise operators, logical operators, and the conditional (ternary) operator in a C
program. Here's a program showcasing each of these concepts:
#include <stdio.h>
void unaryOperatorExample() {
int x = 10;
// Unary operators
void bitwiseOperatorExample() {
int a = 10, b = 5;
// Bitwise AND
// Bitwise OR
// Bitwise XOR
// Bitwise NOT
void logicalOperatorExample() {
int x = 10, y = 5;
// Logical AND
// Logical OR
// Logical NOT
void conditionalOperatorExample() {
int a = 10, b = 5;
int main() {
unaryOperatorExample();
bitwiseOperatorExample();
logicalOperatorExample();
conditionalOperatorExample();
return 0;
Demonstrates pre-increment and pre-decrement operations (++x, --x) on the variable 'x'.
Shows bitwise AND (&), OR (|), XOR (^), and NOT (~) operations between two integers 'a'
and 'b'.
Displays logical AND (&&), OR (||), and NOT (!) operations between two integers 'x' and 'y'.
Utilizes the ternary operator (?:) to find the maximum value between 'a' and 'b' and assigns
it to 'max'. Finally, the main() function calls each of these examples, printing the results to
the consol
SELF-ASSESSMENT QUESTIONS - 1
1. In C programming, which operator is used to perform logical AND between two
expressions?
(a) &&
(b) ||
(c) &
(d) |
2. Which of the following operators is used to perform logical negation in C?
(a) +
(b) -
(c) !
(d) ~
Example:
#include <stdio.h>
if (a > b) {
return a;
} else {
return b;
int main() {
return 0;
In this example, the max() function takes two integer parameters a and b and uses an if
statement to compare them. If a is greater than b, the function returns a, otherwise, it returns
b. The main() function calls max() with two numbers and prints the maximum value. This
illustrates how decision statements can be effectively used within functions to perform
different actions based on specific conditions.
Example:
#include <stdio.h>
return a;
return b;
} else {
return c;
int main() {
return 0;
In this extended example, the maxOfThree() function takes three integer parameters a, b,
and c and uses decision statements to determine the maximum among them. It compares
each number with the others and returns the maximum value. The main() function calls
maxOfThree() with three different numbers (num1, num2, num3) and prints the maximum
value. This demonstrates how decision statements can be used within functions to perform
more complex tasks based on specific conditions.
SELF-ASSESSMENT QUESTIONS - 2
3. Functions with decision statements allow for:
(a) Repeating a block of code
(b) Customizing function behavior based on specific conditions
(c) Declaring arrays and structures
(d) Printing output to the console
4. Which keyword is used to introduce a decision statement in C programming?
(a) decision
(b) if
(c) choose
(d) pick
#include <stdio.h>
int factorial(int n) {
int result = 1;
result *= i;
return result;
int main() {
int num = 5;
return 0;
In this example, the factorial() function takes an integer parameter n and uses a for loop to
calculate the factorial of n. The loop iterates from 1 to n, multiplying each integer along the
way to compute the factorial. The function then returns the final result.
The main() function calls factorial() with a value of num (in this case, 5) and prints the
factorial result. This demonstrates how loop operators, in this case, the for loop, can be
effectively used within functions to perform repetitive tasks, such as calculating factorials, in
C programming.
SELF-ASSESSMENT QUESTIONS - 3
5. What is the purpose of using loop operators within functions in C programming?
(a) To define variables
(b) To perform mathematical operations
(c) To execute a block of code repeatedly based on certain conditions
(d) To print output to the console
6. Which loop operator allows executing a block of code at least once, regardless of
the loop condition?
(a) for loop
(b) while loop
(c) do-while loop
(d) break statement
4. SUMMARY
Unary operators act on a single operand, and functions can implement custom behaviors for
unary operations. Bitwise operators manipulate individual bits of integer operands, with
functions encapsulating bitwise operations for tasks like setting or clearing specific bits.
Logical operators perform boolean operations, aiding decision-making within functions. The
conditional operator offers concise conditional expressions.
Decision statements (if, else if, else) allow functions to make choices based on conditions,
enhancing flexibility. Loop operators (for, while, do-while) enable repetitive code execution
within functions, facilitating data iteration and task repetition. Understanding these
concepts is vital for efficient and structured C programming, empowering developers to
manipulate data and control program flow effectively.
5. TERMINAL QUESTIONS
1. Discuss how loop operators enable functions to execute a block of code repeatedly
and provide examples demonstrating their practical application..
2. Discuss the concept of functions with arrays in C programming. Explain how functions
can accept arrays as arguments and return arrays as results.
3. Provide examples to illustrate how decision statements can be used to make choices
based on different conditions within functions.
1. (a) &&
2. (c) !
3. (b) Customizing function behavior based on specific conditions
4. (b) if
5. (c) To execute a block of code repeatedly based on certain conditions
6. (c) do-while loop
3. #include <stdio.h>
if (num % 2 == 0) {
} else {
int main() {
checkEvenOrOdd(5);
checkEvenOrOdd(10);
return 0;