This is a C program to read two integers & swap their values.
This program reads two integers & swaps their values.
1. Take two integers as input.
2. Use a function for swapping process.
3. Exchange the values using another variable.
4. Print the output and exit.
Here is source code of the C program to read two integers & swap their values. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
* C program to read two integers M and N and to swap their values.
* Use a user-defined function for swapping. Output the values of M
* and N before and after swapping.
void swap(float *ptr1, float *ptr2);
float m, n;
printf("Enter the values of M and N \n");
scanf("%f %f", &m, &n);
printf("Before Swapping:M = %5.2ftN = %5.2f\n", m, n);
printf("After Swapping:M = %5.2ftN = %5.2f\n", m, n);
/* Function swap - to interchanges the contents of two items */
void swap(float *ptr1, float *ptr2)
temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
1. Take the two integers as input and store it in the variables m and n respectively.
2. Call function swap. Pass addresses of variables to the function swap.
3. Receive the addresses by the two pointers ptr1 and ptr2.
4. First copy the value stored at &m to the variable temp.
5. Copy the value stored at &n to &m.
6. Copy the value in the variable temp to &n.
7. Print the variables m and n as output and exit
Enter the values of M and N 2 3 Before Swapping:M = 2.00 N = 3.00 After Swapping:M = 3.00 N = 2.00
Sanfoundry Global Education & Learning Series – 1000 C Programs.