C Program To Swap Two Numbers Using Pointers Without Using Third Variable

C Program To Swap Two Numbers Using Pointers Without Using Third Variable. In this program, we can execute swapping numbers using a pointer for beginners to understand the concept in c.

Temporary Memory in C Program

A temporary variable is a variable in computer programming that has a short lifetime and is used to hold data that will be discarded soon or before it can be stored in a more permanent memory location. 

It is usually declared as a local variable, or a variable with a local scope because it is short-lived. 

As soon as the expression is valid, set a temporary variable to its value. 

In the rest of the method, use the variable instead of the expression.

Pointer Definition in C

In C, a pointer is a variable that keeps track of the address of another variable. 

A pointer can also be used to refer to the function of another pointer. 

A pointer can be incremented or decremented, pointing to the next or previous memory location, respectively. 

The purpose of a pointer is to save memory and speed up the execution process.

C Program To Swap Two Numbers Using Pointers Without Using Third Variable

#include <stdio.h>
int main()
{
int x, y, *a, *b, temp;
printf("Enter The Numbers to Swap\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
a = &x;
b = &y;
temp = *b;
*b = *a;
*a = temp;
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}

Output:

Enter The Numbers to Swap
45
67
Before Swapping
x = 45
y = 67
After Swapping
x = 67
y = 45