Program 57 - CSU1128 - CSE 2026 - Shoolini University

Program for swapping of two numbers entered by the user through call by reference method

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 parameters x and y.

Function Definition

The program defines the swap function:

  • int temp = *x;: Dereferences x and stores its value in temp.
  • *x = *y;: Sets the value pointed to by x to the value pointed to by y.
  • *y = temp;: Sets the value pointed to by y to temp.

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 of a and b.
  • scanf("%d %d", &a, &b);: Reads the values entered by the user and stores them in a and b.

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 of a and b before the swap.

Swap Call

The program calls the swap function to swap the values of a and b:

  • swap(&a, &b);: Calls the swap function, passing the addresses of a and b.

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 of a and b after 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