
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Swap two variables in one line in C/C+
Here is an example of swapping in C language,
Example
#include <stdio.h> int main() { int a = 28, b = 8; a += b -= a = b - a; // method 1 printf("After Swapping : %d\t%d", a, b); (a ^= b), (b ^= a), (a ^= b); // method 2 printf("\nAfter Swapping again : %d\t%d", a, b); return 0; }
Output
After Swapping : 828 After Swapping again : 288
In the above program, there are two variables a and b and initialized with the values 28 and 8 respectively. There are so many methods to swap two numbers in one line and we displayed two methods here.
a += b -= a = b - a; // method 1 printf("After Swapping : %d\t%d", a, b); (a ^= b), (b ^= a), (a ^= b); // method 2 printf("\nAfter Swapping again : %d\t%d", a, b);
- Related Articles
- Swap two variables in one line using C#
- Swap two variables in one line in Java
- Swap two variables in one line in using Java
- Swap two variables in one line in using Python?
- Swap two variables in one line in C/C++, Python, PHP and Java
- How to swap two variables in JavaScript?
- How to Swap Two Variables using Python?
- How to swap two files in Linux command line?
- Swap two numbers in C#
- How to swap two String variables without third variable.
- Largest number with one swap allowed in C++
- How to swap variables with destructuring in JavaScript?
- Meta Strings (Check if two strings can become same after a swap in one string) in C++
- How to swap variables using Destructuring Assignment in JavaScript?
- Swap two Strings without using temp variable in C#

Advertisements