English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The following is an example of swapping two variables.
#include <stdio.h> int main() { int a, b; printf("Enter the value of a: "); scanf("%d", &a); printf("\nEnter the value of b: "); scanf("%d", &b); a += b -= a = b - a; printf("\nAfter Swapping: %d %d", a, b); return 0; }
Output Result
Enter the value of a: 23 Enter the value of b: 43 After Swapping: 4323
In the above program, two variables a and b are declared and dynamically initialized at runtime.
int a, b; printf("Enter the value of a: "); scanf("%d", &a); printf("\nEnter the value of b: "); scanf("%d", &b);
No third variable is used when swapping numbers.
a += b -= a = b - a;