Program 9 - CSU1128 - CSE 2026 - Shoolini University

Program to swap the values by using call by reference.

Code

                    
/*
 * -----------------------------------------------------------
 * Logic Building with Computer Programming (CSU1128P)
 * Instructor: Abdullahi Adem | 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>
void swap(int *d, int *m);
int main()
{
    int j, o;
    printf("\n\n -- This program swaps two values by call by reference --\n\n");
    printf("Enter the value of d and m: ");
    scanf("%d %d", &j, &o);
    printf("\tBefore swapping: d = %d, m = %d\n", j, o);
    swap(&j, &o);
    printf("\tAfter swapping: d = %d, m = %d\n", j, o);
    return 0;
}
// Function to swap two elements using call by reference
void swap(int *d, int *m)
{
    int n = *d;
    *d = *m;
    *m = n;
}
                    
                

Output

-- This program swaps two values by call by reference --
Enter the value of d and m: 43 76
 Before swapping: d = 43, m = 76
 After swapping: d = 76, m = 43