call by reference - Using Pointers and Functions - C Programming
Write a C program to swap (interchange) the value of variables.
/* C Program to swap the value of variables using call by reference */
#include <stdio.h>
main()
{
int a=10, b=20;
printf ("\n Before swap a=%d,b=%d", swap(a,b));
void swap(&a,&b); /* swap is a function call with parameters */
printf ("\n After calling swap function a= %d,b=%d",a,b);
} /* main */
void swap(x,y)
int *x, *y; /* *x, *y is a pointer variables */
{
x=x+y;
y=x‒y;
x=x‒y;
printf ("\n In swap function x=%d,y=%d", *x, *y);
} /* swap */
OUTPUT
Before swap a=10, b=20.
In swap function x=20, y=10.
After calling swap function a=20, y=10.
EXPLANATION: Function called with address of variables a and b. The address of a,b are referred as x,y. The variables x and y is declared as pointer variables. The values of x and y are swapped without third variable and print the pointer variable x and y. The main program prints the value of a and b after called the swap() function.
Computer Programming C Laboratory: Pointers C Programs : Tag: C Programming : - Write a C program to swap (interchange) the value of variables (Using Pointer and call by reference Function)
Computer Programming C Laboratory
CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
English Essentials I
EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
தமிழர் மரபு - Heritage of Tamils
UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Calculus
MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Physics I
PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Chemistry I
CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Makerspace
ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Computer Programming C
CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Computer Programming Python
CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Fundamentals of Electrical and Electronics Engineering
EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation
Introduction to Mechanical Engineering
ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation
Introduction to Civil Engineering
CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation
Essentials of Computing
CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation
Applied Physics I Laboratory
PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Applied Chemistry I Laboratory
CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Computer Programming C Laboratory
CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Computer Programming Python Laboratory
CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Engineering Drawing
ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electronics and Electrical Engineering
EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation
Essentials of Computing - Laboratory
CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation