DEV Community

Cover image for Swap two integers in 'C' programming Without using third variable

Posted on


Swap two integers in 'C' programming Without using third variable

C program to swap two integers without using third variable

Top comments (4)

fpuffer profile image
Frank Puffer • Edited

While it does have some educational value to show that this is possible, this method has two issues:

  • Both a = a + b and a = a - b can cause an overflow.

  • I can't imagine many situations where saving a few bytes really makes up for the loss in readability.

semiversus profile image
Günther Jena • Edited

It also works with XOR (avoiding the overflow problem):

a = a ^ b
b = a ^ b
a = a ^ b
Enter fullscreen mode Exit fullscreen mode
codemouse92 profile image
Jason C. McDonald

Agreed, this is definitely the way to do it.

joecar7 profile image
Joe Carnuccio • Edited

reduced to one line:

a ^= b ^= a ^= b;

An Animated Guide to Node.js Event Loop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.