Module 4 Important Questions
Module 4 Important Questions
⭐⭐1. Define formal parameters and actual parameters. Illustrate with an example. (3
marks and above)
Ans: Formal parameters, also known as formal arguments, are variables listed in a function's
definition. They act as placeholders for the values that will be passed to the function when it
is called.
Actual parameters, also called actual arguments, are the values that are passed to the
function when it is called.
Eg: #include <stdio.h>
// Function declaration with formal parameter 'num'
void printNumber(int num);
int main() {
int x = 10; // Actual parameter
printNumber(x); // Calling the function with 'x' as the actual
parameter
return 0;
}
// Function definition with formal parameter 'num'
void printNumber(int num) {
printf("The number is: %d\n", num);
}
In this example, `num` is the formal parameter of the `printNumber` function. When the
function is called with `x` as the actual parameter, the value of `x` (which is 10) is passed to
the function and assigned to the `num` formal parameter. The function then prints "The
number is: 10" using the value of the formal parameter.
⭐⭐2. What is modular programming? What are its advantages? (3 marks and above)
OR
What are the advantages of using functions in a program?
Ans: Modular programming in C involves breaking code into separate functions or modules,
each responsible for a specific task, promoting reusability and easier maintenance.
Advantages of modular programming:
1. **Easy to Understand**: Breaks complex problems into smaller, manageable parts,
making the code easier to comprehend and maintain.
2. **Reusability**: Modules can be reused in different parts of the program or in other
projects, saving time and effort.
3. **Collaboration**: Multiple programmers can work on different modules simultaneously,
enhancing teamwork and speeding up development.
4. **Debugging**: Isolates errors to specific modules, simplifying the process of finding and
fixing bugs.
5. **Scalability**: Allows for easy expansion by adding or modifying modules, ensuring the
system remains flexible and adaptable.
⭐⭐6. What is the purpose of function declaration, function definition and function
call? With examples illustrate their syntax. (7 marks)
Ans: Function declaration, function definition, and function call are all essential concepts in
programming that allow you to create reusable and organized code.Explanation and an
example for each:
1. *Function Declaration:*
A function declaration tells the compiler or interpreter about the existence of a function, its
name, return type, and the types of its parameters. It doesn't provide the implementation of
the function; it's like a promise that the function will be defined later.
*Example of Function Declaration:*
// Function declaration
int add(int a, int b);
2. *Function Definition:*
A function definition provides the actual implementation of the function. It contains the
code that will be executed when the function is called. It specifies the logic, operations, and
return value of the function.
*Example of Function Definition:*
// Function definition
int add(int a, int b) {
return a + b;
}
3. *Function Call:*
A function call is used to execute the code within the function. When a function is called, the
program jumps to the corresponding function definition, executes the code there, and then
returns to the point where the function was called.
Example code in C:
#include <stdio.h>
// Function declaration
int add(int a, int b);
int main() {
// Function call
int result = add(5, 3);
printf("Result: %d\n", result);
return 0;
}
// Function definition
int add(int a, int b) {
return a + b;
}
In this example, we have a function named `add` that takes two integers as parameters and
returns their sum. The function is declared at the beginning, defined later in the code, and
then called within the `main` function. The function call `add(5, 3)` calculates the sum of 5
and 3, which is 8, and the result is printed to the console.
Function declaration, definition, and call allow you to modularize your code, making it easier
to read, maintain, and debug, as well as promoting code reuse across different parts of your
program.
⭐7. Name the different types of parameter passing. Illustrate each of them with an
example.
(3 marks and above).
Ans: 1. **Pass by Value:**
In pass by value, a copy of the variable's value is passed to the function, so changes inside
the function don't affect the original variable.
Eg: #include <stdio.h>
void square(int num) {
num = num * num;
}
int main() {
int x = 5;
square(x);
printf("x: %d\n", x); // Output: x: 5
return 0;
}
2. **Pass by Reference (Pass by Pointer):**
In pass by reference, a pointer to the variable is passed to the function, allowing changes
inside the function to affect the original variable.
Eg: #include <stdio.h>
void increment(int *numPtr) {
(*numPtr)++;
}
int main() {
int y = 7;
increment(&y);
printf("y: %d\n", y); // Output: y: 8
return 0;
}
In the pass by value example, the function `square` doesn't modify the original `x` value. In
the pass by reference example, the function `increment` uses a pointer to modify the
original `y` value.
⭐8. Write a C program to : (i) Create a structure containing the fields: Name, Price,
Quantity, Total Amount. (ii) Use separate functions to read and print the data. (7 marks)
Ans: #include <stdio.h>
// Define the structure
struct item {
char name[20];
float price;
int quantity;
float total_amount;
};//created structure as per the first subquestion
⭐9. What is recursion? Write a C program to display Fibonacci series using recursive
function.
(7 marks)
Ans: Recursion is a programming technique where a function calls itself to solve a smaller
subproblem, eventually leading to the solution of the larger problem. In other words, a
function can be defined in terms of itself.
Program in C to find the Fibonacci series using a recursive function:
#include <stdio.h>
// Recursive function to calculate the nth Fibonacci number
int fibonacci(int n) {
if (n <= 1)
return n;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Fibonacci series up to %d terms:\n", n);
for (int i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
return 0;
}
In this program, the `fibonacci` function calculates the nth Fibonacci number using
recursion. The base cases are when `n` is 0 or 1, in which case the function returns `n`.
Otherwise, it returns the sum of the (n-1)th and (n-2)th Fibonacci numbers, which are
calculated recursively.
⭐10. Write a C program to: (i) Create a structure with fields: Name, Address, Date of birth.
(ii) Read the above details for five students from user and display the details. (7 marks)
Ans: #include <stdio.h>
// Structure definition for student details
struct Student {
char name[50];
char address[100];
char dob[15];
};//Answer for sub question 1
int main() {
// Declare an array of struct Student to hold details of five
students
struct Student students[5];
printf("Enter details for five students:\n");
for (int i = 0; i < 5; i++) {
printf("Student %d:\n", i + 1);
printf("Name: ");
scanf("%s", students[i].name); // Using %s assuming name
doesn't contain spaces
printf("Address: ");
scanf(" %[^\n]", students[i].address); // Allowing spaces in
address
printf("Date of Birth: ");
scanf("%s", students[i].dob);
printf("\n");
}
printf("\nDisplaying student details:\n");
for (int i = 0; i < 5; i++) {
printf("Student %d:\n", i + 1);
printf("Name: %s\n", students[i].name);
printf("Address: %s\n", students[i].address);
printf("Date of Birth: %s\n", students[i].dob);
printf("\n");
}
return 0;
}
⭐⭐11. What are different storage classes in C? Give examples for each. (7 marks)
Ans: There are four storage classes in C:
1. Automatic Storage Classes in C
Every variable defined in a function or block belongs to automatic storage class by default
if there is no storage class mentioned. The variables of a function or block belong to the
automatic storage class are declared with the auto specifier. Variables under auto in C are
local to the block where they are defined and get discarded outside the block.
int main( )
321
Explanation:
In the above program, there is three times the variable i is declared. Variables with the
same name can be defined in different blocks. Thus, this program will compile and
execute successfully without any error. The function ‘printf’ in the innermost block will
print 3 and the variable i in this block will be destroyed after the block ends.
The next one, the second outer block prints 2 which is then succeeded by the outer block
which prints 1. The automatic variables are initialized properly; else you will get
undefined values as the compiler does not give them an initial value.
The variables belonging to a register storage class are equivalent to auto in C but are
stored in CPU registers and not in the memory, hence the name. They are the ones
accessed frequently. The register specifier is used to declare the variable of the register
storage class. Variables of a register storage class are local to the block where they are
defined and destroyed when the block ends.
#include <stdio.h>
int main()
Explanation:
In the above program, the code tries to get the address of variable i into the pointer
variable p but as i is declared as a register variable, the code won’t compile and will
display the error ” Error: address of register variable requested”.
Only certain types of variables are placed into registers. Register variables are not given
an initial value by the compiler.
3. Static Storage Classes in C
The visibility of static variables is zero outside their function or file, but their values are
maintained between calls. The variables with static storage class are declared with the
static specifier. Static variables are within a function or file. The static specifier works
differently with local and global variables.
A static storage class will instruct a compiler for keeping the local variable existing during
the span of a program in place of creating and then destroying the same every time the
scope arises around it. Thus, making all local variables static will allow the maintenance of
the values between all function calls.
A static modifier gets applied to all global variables. Once done, this causes that particular
variable scope restriction to a file where the same is declared.
The static specifier in programming works differently with both local and global
variables. Take a look below to understand the static storage class in c.
Simple Programs Showing Static Storage Classes with Local and Global Variables:
i. Local Variable
#include <stdio.h>
void staticDemo()
static int i;
static int i = 1;
printf(“%d “, i);
i++;
}
printf(“%d”, i);
i++;
int main()
staticDemo();
staticDemo();
10
21
Explanation:
#include <stdio.h>
{
static int i;
printf(“%d “, i);
i++;
printf(“%d”, globalInt);
globalInt++;
int main()
staticDemo();
staticDemo();
01
12
Explanation:
Static variables need to be initialized only once in a program and they are retained
throughout the lifetime. They have a default initial value of zero.
When a global variable or function is defined by a static specifier, then that variable or
function is known only to the file in which it is defined. For a global variable, other file
routines cannot access and alter its contents as a static global variable has internal
linkage. In the above program, the static global variable globalInt and a static function
staticDemo(), are defined as static and they cannot be used outside the C file.
4. External Storage Classes in C
External storage class variables or functions are declared by the ‘extern’ specifier. When a
variable is declared with extern specifier, no storage is allotted to the variable and it is
assumed that it has been already defined elsewhere in the program. With an extern
specifier, the variable is not initialized. The reason why extern is used to specify a variable
in a program to declare it with external linkage.
#include <stdio.h>
extern int i;
int main()
int i = 1;
Explanation:
In the above C program, if extern int i is removed, there will be an error “Undeclared
identifier ‘i’ because the variable i is defined after being used in printf. The extern
specifier instructs the compiler that variable i has been defined and is declared here.If you
change extern int i; to extern int i = 5; you will get an error “Redefinition of ‘i'” because
the extern specifier does not initialize a variable.
int main() {
return 0;
In this example:
- `globalVar` has file scope and exists throughout the entire program.
- `localVar` is defined within the `main()` function block and has block scope limited to
that function.
- `loopVar` is defined within the loop block and has block scope limited to the loop.
- The commented `printf` statements outside their respective scopes would result in
errors because the variables are not in scope.