using pointers - C Programming
Write a C program to interchange the values stored in two variables using function with pointers.
The below program shows the contents of two variables, which can be interchanged using their address locations.
The function interchange() receives the addresses of the two variables 'a' and 'b' and exchanges their contents.
/* Program to interchange the values stored in two variables using function with pointers */
#include <stdio.h>
main()
{
/* Local definitions */
void interchange(int *, int *); /* interchange is a function with integer pointer variable */
int a=39, b=77;
/* Statements */
printf("\nBefore Interchange: a = %d, b = %d\n",a,b);
interchange(&a,&b); /* function call */
printf("After Interchange : a = %d, b %d, b = %d\n",a,b);
} /* main */
void interchange (int *a, int *b)
{
/* Local definitions */
int t;
t=*a;
*a= *b;
*b=t;
} /* interchange */
OUTPUT
Before Interchange: a = 39, b = 77
After Interchange : a = 77, b = 39
EXPLANATION: In the above program the variable a is initialised as value 39 and b is initialised as value 77, then prints the values of a and b then call the user defined function interchange() using address location of a and b. In the user‒defined function variable a and b is declared as pointer variable. The body of function interchange the value a to b and b to a using third variable t then control transfer to the main program and prints the value of a and b after interchanged.
Computer Programming C Laboratory: Pointers C Programs : Tag: C Programming : - Write a C program to interchange the values stored in two variables using function with pointers
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