Exercise: Swap Two Numbers Using Pointers

Write code to solve the problem.

Question

Write a C program that swaps the values of two integers using pointers.

Create a function named swap() that takes two pointers to integers and swaps their values. Your program should print the values before and after swapping.

Expected output:

Before swap: a = 5, b = 10
After swap: a = 10, b = 5

Exercise: Swap Two Numbers Using Pointers

Write code to solve the problem.

Question

Write a C program that swaps the values of two integers using pointers.

Create a function named swap() that takes two pointers to integers and swaps their values. Your program should print the values before and after swapping.

Expected output:

Before swap: a = 5, b = 10
After swap: a = 10, b = 5
C
#include <stdio.h>
/* Write the swap function here */
int main() {
int a = 5;
int b = 10;
printf("Before swap: a = %d, b = %d\n", a, b);
/* Call the swap function */
printf("After swap: a = %d, b = %d\n", a, b);
return 0;
}