Ad Space
This question is also covered in Q.59, 61, and 63. Here is a detailed explanation:
Call by Value:
Call by Reference:
Example (Swapping two numbers):
#include <stdio.h>
// Function for Call by Value (will NOT work)
void swap_by_value(int a, int b) {
int temp = a;
a = b;
b = temp;
printf("Inside swap_by_value: a = %d, b = %d\n", a, b);
}
// Function for Call by Reference (WILL work)
void swap_by_reference(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
printf("Inside swap_by_reference: a = %d, b = %d\n", *a, *b);
}
int main() {
int x = 10, y = 20;
// Call by Value
printf("Before swap_by_value: x = %d, y = %d\n", x, y);
swap_by_value(x, y);
printf("After swap_by_value: x = %d, y = %d\n\n", x, y); // x and y are unchanged
// Reset values
x = 10;
y = 20;
// Call by Reference
printf("Before swap_by_reference: x = %d, y = %d\n", x, y);
swap_by_reference(&x, &y); // Pass addresses
printf("After swap_by_reference: x = %d, y = %d\n", x, y); // x and y are swapped
return 0;
}
This question is also covered in Q.62 and Q.64.
Dynamic Memory Allocation is the process of allocating memory at runtime, rather than at compile time. This is essential when the exact amount of memory needed is not known until the program is running (e.g., based on user input).
The memory is allocated from a system memory area called the heap. All functions for dynamic memory allocation are found in the <stdlib.h> header file.
Key Functions:
malloc(size_t size): Allocates a single block of memory of the specified size (in bytes). It returns a void* pointer to the first byte of the allocated block, or NULL if allocation fails. The memory block is not initialized (contains garbage values).calloc(size_t n, size_t size): Allocates memory for an array of n elements, each of size bytes. It returns a void* pointer. The allocated memory is initialized to zero.realloc(void *ptr, size_t new_size): Resizes a previously allocated memory block (pointed to by ptr) to a new_size. It preserves the content of the old block.free(void *ptr): Deallocates (releases) a block of memory previously allocated by malloc, calloc, or realloc, returning it to the heap.Example (Allocating an array for 'n' integers):
#include <stdio.h>
#include <stdlib.h> // For malloc and free
int main() {
int n, i, sum = 0;
int *ptr;
printf("Enter the number of elements: ");
scanf("%d", &n);
// Dynamically allocate memory for n integers
ptr = (int*) malloc(n * sizeof(int));
// Check if memory was allocated
if (ptr == NULL) {
printf("Memory allocation failed!\n");
return 1; // Exit with an error
}
printf("Enter %d numbers:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", ptr + i); // Same as &ptr[i]
sum += *(ptr + i); // Same as ptr[i]
}
printf("The numbers are: ");
for (i = 0; i < n; i++) {
printf("%d ", *(ptr + i));
}
printf("\nSum = %d\n", sum);
// Free the allocated memory
free(ptr);
return 0;
}
A recursive function is a function that calls itself to solve a problem. To find a factorial, the base case is factorial(0) = 1, and the recursive step is factorial(n) = n * factorial(n-1).
#include <stdio.h>
// Recursive function to find factorial
long long factorial(int n) {
// Base Case
if (n == 0 || n == 1) {
return 1;
}
// Recursive Step
else {
return n * factorial(n - 1);
}
}
int main() {
int num;
printf("Enter a non-negative number: ");
scanf("%d", &num);
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
printf("Factorial of %d = %lld\n", num, factorial(num));
}
return 0;
}
auto and register are storage class specifiers.
auto: It is the default storage class for all local variables. It specifies that the variable has a local scope and is created when the block is entered and destroyed when the block is exited. Using the auto keyword is optional.register: It is a hint to the compiler to store the variable in a CPU register instead of memory (RAM) for faster access. This is often used for frequently accessed variables like loop counters. The compiler can ignore this request. You cannot get the address (using &) of a register variable.
#include <stdio.h>
int main() {
// 1. 'auto' storage class
// This is the default for local variables, so 'auto' is redundant.
auto int a = 10; // Same as 'int a = 10;'
{
auto int b = 20; // 'b' is local to this block
printf("Inside block: a = %d, b = %d\n", a, b);
}
// printf("Outside block: b = %d\n"); // Error: 'b' is not defined here
// 2. 'register' storage class
// Requesting the compiler to store 'i' in a CPU register
register int i;
long sum = 0;
printf("Illustrating 'register' keyword:\n");
for (i = 0; i <= 10000; i++) {
sum += i;
}
printf("Sum of first 10000 numbers = %ld\n", sum);
// int *ptr = &i; // Error: Cannot take address of register variable
return 0;
}
This program uses a structure to hold the (x, y) coordinates of a point and calculates the distance using the Euclidean distance formula: $d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}$.
#include <stdio.h>
#include <math.h> // For sqrt() and pow()
// Define a structure for a point
struct Point {
float x;
float y;
};
// Function to calculate distance
float calculateDistance(struct Point p1, struct Point p2) {
float distance;
distance = sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2));
return distance;
}
int main() {
struct Point point1, point2;
float dist;
printf("Enter coordinates for Point 1 (x1 y1): ");
scanf("%f %f", &point1.x, &point1.y);
printf("Enter coordinates for Point 2 (x2 y2): ");
scanf("%f %f", &point2.x, &point2.y);
dist = calculateDistance(point1, point2);
printf("The distance between the two points is: %.2f\n", dist);
return 0;
}
#include <stdio.h>
// Function to check if a number is odd or even
// It takes one integer argument
void checkOddEven(int num) {
if (num % 2 == 0) {
printf("%d is an Even number.\n", num);
} else {
printf("%d is an Odd number.\n", num);
}
}
int main() {
int n;
printf("Enter an integer: ");
scanf("%d", &n);
// Call the function to check the number
checkOddEven(n);
return 0;
}
This program uses the simple Bubble Sort algorithm to sort an array of 'N' numbers entered by the user in ascending order.
#include <stdio.h>
int main() {
int n, i, j, temp;
int arr[100]; // Assuming max 100 elements
printf("Enter the number of elements (N): ");
scanf("%d", &n);
printf("Enter %d numbers:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Bubble Sort Algorithm
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("Sorted array in ascending order:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
This program uses a function with pointers (pass by reference) to swap the values of two variables located in the main function.
#include <stdio.h>
// Function to swap two numbers using pointers (pass by reference)
void swap(int *num1, int *num2) {
int temp;
temp = *num1; // Store the value at address num1
*num1 = *num2; // Store the value at address num2 into address num1
*num2 = temp; // Store the temporary value into address num2
}
int main() {
int a, b;
printf("Enter value for a: ");
scanf("%d", &a);
printf("Enter value for b: ");
scanf("%d", &b);
printf("Before swapping: a = %d, b = %d\n", a, b);
// Pass the addresses of 'a' and 'b' to the swap function
swap(&a, &b);
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
Ad Space