Explanation
The program demonstrates swapping two numbers entered by the user using the call by reference method with pointers.
Header
The code begins with including the standard input-output header file using #include <stdio.h>.
Function Declaration
The program declares a function swap to swap two integers using pointers:
void swap(int *x, int *y);: Declares the function with two integer pointer parametersxandy.
Function Definition
The program defines the swap function:
int temp = *x;: Dereferencesxand stores its value intemp.*x = *y;: Sets the value pointed to byxto the value pointed to byy.*y = temp;: Sets the value pointed to byytotemp.
Variable Declaration
The main() function is defined, and two variables are declared:
int a, b;: Variables to hold the values entered by the user.
Program Description
The program prints a description message for the user to understand its functionality.
User Input
The user is prompted to enter the values of a and b:
printf("Enter the value for a and b: ");: Prompts the user to enter the values ofaandb.scanf("%d %d", &a, &b);: Reads the values entered by the user and stores them inaandb.
Before Swap
The program prints the values of a and b before swapping:
printf("Before: a = %d, b = %d\n", a, b);: Prints the values ofaandbbefore the swap.
Swap Call
The program calls the swap function to swap the values of a and b:
swap(&a, &b);: Calls theswapfunction, passing the addresses ofaandb.
After Swap
The program prints the values of a and b after swapping:
printf("After: a = %d, b = %d\n", a, b);: Prints the values ofaandbafter the swap, showing that they have been successfully swapped.
Program End
The program ends with return 0; to indicate successful execution.
Code
/*
* -----------------------------------------------------------
* Logic Building with Computer Programming (CSU1128)
* Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
*
* This code is a part of the educational initiative by dmj.one
* with aim of empowering and inspiring learners in the field of
* Computer Science and Engineering through the respective courses.
*
* (c) 2022, Divya Mohan for dmj.one. All rights reserved.
* -----------------------------------------------------------
*/
#include <stdio.h>
// Function to swap two integers using pointers (Call by Reference)
void swap(int *x, int *y) {
int temp = *x; // Dereference 'x' and store its value in 'temp'
*x = *y; // Set the value pointed to by 'x' to the value pointed to by 'y'
*y = temp; // Set the value pointed to by 'y' to 'temp'
}
int main() {
printf("\n\n Program for swapping of two numbers entered by the user through call by reference method. \n\n");
int a, b; // Declare two integers 'a' and 'b'
// Get the values of 'a' and 'b' from the user
printf("Enter the value for a and b: ");
scanf("%d %d", &a, &b);
// Display the initial values of 'a' and 'b'
printf("Before: a = %d, b = %d\n", a, b);
// Call the 'swap' function, passing the addresses of 'a' and 'b'
swap(&a, &b);
// Display the swapped values of 'a' and 'b'
printf("After: a = %d, b = %d\n", a, b);
return 0; // Return 0 to indicate successful execution
}
Output
Program for swapping of two numbers entered by the user through call by reference method.
Enter the value for a and b: 12 43
Before: a = 12, b = 43
After: a = 43, b = 12