Foc - unit-III Question Bank With Answer
Foc - unit-III Question Bank With Answer
PART A (2 Marks)
2. Reusability
3. Organization
4. Ease of Maintenance
5. Abstraction
6. Scope Management
7. Parameterization
8. Recursion
Declaring a Pointer
data_type *pointer_name;
- data_type: This specifies the type of data the pointer will point to (e.g., `int`, `float`, `char`, etc.).
Example
An array is a data structure in C that allows you to store a fixed-size sequential collection of elements
of the same type. Each element can be accessed using an index, making arrays efficient for storing
and manipulating lists of data.
#include <stdio.h>
int main() {
1
float numbers[5];
numbers[0] = 1.5;
numbers[1] = 2.5;
numbers[2] = 3.5;
numbers[3] = 4.5;
numbers[4] = 5.5;
return 0;
Definition
A self-referential structure is a structure that includes a member that is a pointer to the same
type of structure. This enables the creation of dynamic data structures where each element
can point to another element of the same type.
Key Features
2
Part –B
Pointers and addresses are fundamental concepts in C programming that work together to
manage memory and facilitate data manipulation. Here’s an overview of both:
Pointers
Definition: A pointer is a variable that stores the memory address of another variable. It
allows programmers to directly access and manipulates the memory where data is stored.
Initialization: Pointers are typically initialized by assigning them the address of a variable
using the address-of operator (`&`).
Dereferencing: To access the value stored at the address a pointer points to, you use the
dereference operator (`*`).
For example, `value = *ptr;` retrieves the value stored at the address held by `ptr`.
Addresses
Memory Layout: When a program runs, memory is divided into different sections, such as
the stack (for local variables) and the heap (for dynamically allocated memory). Each section
has its own addresses.
Address Size: The size of an address depends on the architecture of the system (e.g., 32-bit
or 64-bit). In a 32-bit system, addresses are typically 4 bytes long, while in a 64-bit system,
they are 8 bytes.
1. Accessing Memory: Pointers use addresses to directly access and manipulate data in
memory. By storing the address of a variable, a pointer provides a way to reference and
modify the value at that location.
3
2. Dynamic Memory: Pointers are essential for dynamic memory management. Functions
like `malloc` and `free` return pointers to memory blocks, enabling the allocation and
deallocation of memory at runtime.
3. Efficient Data Structures: Pointers allow the creation of complex data structures that
rely on addresses for linking elements (e.g., linked lists). Each node in such structures
contains a pointer to the next node, facilitating traversal and manipulation.
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
// Input string
if (isalpha(ch)) {
// Check if it is a vowel
vowels++;
} else {
// It is a consonant
consonants++;
4
}
return 0;
numbers.
1. Function Declaration
A function declaration provides the compiler with the function's name, return type, and
parameters (if any). It does not include the body of the function.
Syntax:
Example:
2. Function Definition
The function definition includes the actual implementation of the function, including its body.
Syntax:
// function body
5
}
Example:
return a;
return b;
} else {
return c;
3. Function Call
A function call is how you execute the function in your code, providing the necessary
arguments.
Syntax:
Example:
Here is a complete C program that includes function declaration, definition, and calling to
find the biggest of three numbers:
#include <stdio.h>
// Function declaration
int main() {
6
// Input from the user
// Function call
return 0;
// Function definition
return a;
return b;
} else {
return c;
7
12.B( ii). Inspect recursive function and its significance. Develop a C
Recursive Functions in C
A recursive function is a function that calls itself to solve smaller instances of the same
problem. Recursion is often used in situations where a problem can be broken down into
smaller, similar subproblems, making it a natural fit for problems like calculating factorials,
generating Fibonacci numbers, traversing trees, and more.
1. Simplicity: Recursive functions can simplify the code, making it easier to understand and
maintain, especially for problems that have a clear recursive structure.
2. Natural Fit for Certain Problems: Some algorithms (like traversing a binary tree) are more
straightforward when expressed recursively.
3. Reduction in Code Size: Recursive solutions can often be written in fewer lines than their
iterative counterparts.
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1) {
int main() {
int m;
scanf("%d", &m);
8
printf("First %d Fibonacci numbers are:\n", m);
printf("\n");
return 0;
Output
When you run this program, it will ask for the number of Fibonacci numbers you want to
generate, and then it will display them. For example, if you input `5`, the output will be:
0 1123
Arrays in C
An array is a collection of variables of the same type, stored in contiguous memory locations.
In C, arrays can be single-dimensional (1D) or multi-dimensional (such as two-dimensional,
or 2D).
Single-Dimensional Arrays
Declaration:
data_type array_name[size];
Initialization:
9
Example:
Two-Dimensional Arrays
Declaration:
data_type array_name[rows][columns];
Initialization:
data_type array_name[rows][columns] = {
...
};
Example:
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
#include <stdio.h>
#define ROWS 3
#define COLS 3
10
}
int main() {
scanf("%d", &first[i][j]);
scanf("%d", &second[i][j]);
// Add matrices
11
for (int j = 0; j < COLS; j++) {
printf("\n");
return 0;
Example Output
Element [0][0]: 1
Element [0][1]: 2
Element [0][2]: 3
Element [1][0]: 4
Element [1][1]: 5
Element [1][2]: 6
Element [2][0]: 7
Element [2][1]: 8
Element [2][2]: 9
Element [0][0]: 9
Element [0][1]: 8
Element [0][2]: 7
Element [1][0]: 6
Element [1][1]: 5
Element [1][2]: 4
12
Element [2][0]: 3
Element [2][1]: 2
Element [2][2]: 1
10 10 10
10 10 10
10 10 10
In C programming, storage classes define the scope (visibility) and lifetime of variables and
functions. They determine how and where the variables are stored, which affects memory
management, performance, and variable accessibility.
1. Scope: Defines where the variable can be accessed within the code.
2. Lifetime: Determines how long the variable exists in memory during program execution.
1. Automatic (`auto`): Default storage class for local variables. Scope is limited to the block
in which they are defined, and they are created and destroyed automatically.
2. Register: Suggests to the compiler to store the variable in a register for faster access. It is
still local in scope and has automatic lifetime.
3. Static: Maintains the variable’s value between function calls. Scope is limited to the block,
but the lifetime extends across the entire program execution.
4. External (`extern`): Used to declare a variable that is defined in another file or outside the
current scope. The variable can be accessed across different files.
13
Examples
#include <stdio.h>
void demoAuto() {
int main() {
demoAuto();
return 0;
#include <stdio.h>
void demoRegister() {
int main() {
demoRegister();
return 0;
14
# 3. Static Storage Class
#include <stdio.h>
void demoStatic() {
counter++;
int main() {
demoStatic();
return 0;
#include <stdio.h>
void demoExtern() {
int main() {
demoExtern();
return 0;
15
Comparison of Storage Classes
|---------------|------------------------------|----------------------------|--------------------- |
| `register` | Local to the block | Exists during block execution | Register (if possible)
| `static` | Local to the block (or file) | Exists for the lifetime of the program | Static memory
| `extern` | Global (across files) | Exists for the lifetime of the program | Static memory |
To display all prime numbers between two intervals in C using functions, you can follow
these steps:
Example Program
#include <stdio.h>
if (num <= 1) {
if (num % i == 0) {
16
}
return 1; // Prime
if (isPrime(i)) {
printf("\n");
int main() {
scanf("%d", &lower);
scanf("%d", &upper);
displayPrimes(lower, upper);
return 0;
17
Example Output
11 13 17 19 23 29
18