C language important questions
C language important questions
Functions can be invoked in two ways: Call by Value or Call by Reference. These two ways are
generally differentiated by the type of values passed to them as parameters.
• Call by value - In call by value method of parameter passing, the values of actual parameters
are copied to the function’s formal parameters.
o There are two copies of parameters stored in different memory locations.
o One is the original copy and the other is the function copy.
o Any changes made inside functions are not reflected in the actual parameters of the
caller.
#include <stdio.h>
// Function Prototype
// Main function
int main()
// Pass by Values
return 0;
// two values
int t;
t = x;
x = y;
y = t;
• Call by reference - In call by reference method of parameter passing, the address of the
actual parameters is passed to the function as the formal parameters. In C, we use pointers to
achieve call-by-reference.
o Both the actual and formal parameters refer to the same locations.
o Any changes made inside the function are actually reflected in the actual parameters
of the caller.
#include <stdio.h>
// Function Prototype
void swapx(int*, int*);
// Main function
int main()
{
int a = 10, b = 20;
// Pass reference
swapx(&a, &b); // Actual Parameters
return 0;
}
t = *x;
*x = *y;
*y = t;
Dynamic Memory Allocation is the process of allocating memory to variables at runtime (while
the program is executing) instead of at compile time. It allows programs to manage memory
efficiently by requesting and releasing memory as needed, making it suitable for handling data
structures like arrays, linked lists, and trees whose sizes may change during execution.
Key Features:
• Memory is allocated from the heap instead of the stack.
• The size of memory blocks can be specified dynamically.
• Functions like malloc(), calloc(), realloc(), and free() in C are used to manage dynamic memory.
malloc()
• The malloc (memory allocation) function is used to allocate a specified number of bytes in
memory. When memory is allocated successfully, it returns a pointer to that block,
otherwise it returns NULL.
Syntax – (datatype*) malloc(size*sizeof(datatype));
Example - int* ptr = (int*) malloc(5 * sizeof(int)); // Allocates memory for 5 integers
Calloc()
• The calloc() function in C is used for dynamic memory allocation. Unlike malloc, which leaves
the memory uninitialized, calloc initializes all allocated bytes to zero.
realloc()
• The realloc (reallocate) function allows you to resize a previously allocated memory block.
It takes a pointer to the existing block and the new size in bytes as arguments. If
reallocation is successful, it returns a pointer to the reallocated memory. Otherwise, it
returns NULL.
Syntax – (datatype*)realloc(ptr,size*sizeof(datatype));
Example
free()
int main() {
// variable declaration
int *A,r,size;
// input
A = (int*)malloc(size*4);
// array input
for(r=0;r<size;r++)
scanf("%d",A+r);
printf("\n");
//output
for(r=0;r<size;r++)
printf("%d ",*(A+r));
// free memory
free(A);
return 0;
Syntax:-
return_type function_name(parameters) {
if (base_case_condition) {
// Base case: Stop recursion
return value;
} else {
// Recursive case: Function calls itself
return function_name(smaller_problem);
}
}
Conditions for Recursion to Work
1. Base Case:
o There must be a clear stopping condition to avoid infinite recursion.
2. Progress Toward Base Case:
o Each recursive call must make progress toward the base case (e.g., reducing the size of the
problem).
3. Well-Defined Problem:
o The problem should be divisible into smaller sub-problems that resemble the original
problem.
Q – Write difference between static and dynamic memory allocation ( PYQ RGPV )
int main() {
// variable initialization + declaration
int a = 5,b=3,c=0;
// pointer arithmetic
return 0;
}
Q – Write about pointers in C language
Pointers in C
A pointer is a variable that stores the memory address of another variable. Pointers are powerful
tools in C that allow direct memory access and manipulation.
Key Features:
Syntax:
• c
• Copy code
• data_type *pointer_name;
Example:
#include <stdio.h>
int main() {
return 0;
Types of Pointers in C:
1. Null Pointer:
2. Void Pointer:
o A generic pointer that can point to any data type.
3. Dangling Pointer:
o A pointer that points to a memory location that has been freed or is no longer valid.
4. Wild Pointer:
5. Constant Pointer:
o Example: int num = 10, num2 = 20; int *const ptr = #
6. Pointer to Constant:
7. Function Pointer:
8. Double Pointer:
• A near pointer is a pointer that contains only the offset address within a segment.
• Can access memory only within the current segment (64 KB).
Far Pointer:
• A far pointer is a pointer that contains both the segment address and the offset address.