UrbanPro

Learn C Language from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

How do I swap two variables in one line in C/C++, Python and Java?

Asked by Last Modified  

Follow 4
Answer

Please enter your answer

Advance Excel And VBA Training

How to swap in a single line without using library function? Python: In Python, there is a simple and syntactically neat construct to swap variables, we just need to write “x, y = y, x”. C/C++: Below is one generally provided classical solution // Swap using bitwise XOR (Wrong Solution in C/C++) x...
read more
How to swap in a single line without using library function? Python: In Python, there is a simple and syntactically neat construct to swap variables, we just need to write “x, y = y, x”. C/C++: Below is one generally provided classical solution // Swap using bitwise XOR (Wrong Solution in C/C++) x ^= y ^= x ^= y; The above solution is wrong in C/C++ as it causes undefined behaviour (compiler is free to behave in any way). The reason is, modifying a variable more than once in an expression causes undefined behaviour if there is no sequence point between the modifications. However, we can use comma to introduce sequence points. So the modified solution is // Swap using bitwise XOR (Correct Solution in C/C++) // sequence point introduced using comma. (x ^= y), (y ^= x), (x ^= y); Java: In Java, rules for subexpression evaluations are clearly defined. The left hand operand is always evaluated before right hand operand. In Java, the expression “x ^= y ^= x ^= y;” doesn’t produce the correct result according to Java rules. It makes x = 0. However, we can use “x = x ^ y ^ (y = x);” Note the expressions are evaluated from left to right. If x = 5 and y = 10 initially, the expression is equivalent to “x = 5 ^ 10 ^ (y = 5);”. Note that we can’t use this in C/C++ as in C/C++, it is not defined whether left operand or right operand is executed for any operator. Output: After Swapping values of x and y are 10 5 read less
Comments

Web Developer

you can use this following example : int main() { int x = 5, y = 10; (x ^= y), (y ^= x), (x ^= y); printf("After Swapping values of x and y are %d %d", x, y); return 0; }
Comments

Advance Excel And VBA Training

(x ^= y), (y ^= x), (x ^= y);
Comments

View 1 more Answers

Related Questions

what is syntex error
In computer science, a syntax error ( not Syntex ) is an error in the syntax of a sequence of characters or tokens that is intended to be written in a particular programming language. For compiled languages,...
Tanha
How and where do I start learning C programming?
Goals Fundamental concepts of C programming language. Write your first C program. Understand different keywords & Data types in C. Understand Variables & Scope of Variables. Learn Enumeration or Enum...
Krishna
0 0
5
What are the main characteristics of c programming?
C programming is characterized by several key features that contribute to its popularity and versatility. Here are some of the main characteristics of the C programming language: Procedural Programming: ...
Balendra
0 0
6
Why is the C language important?
C language is a high-level, general-purpose programming language. It provides a straightforward, consistent, powerful interface for programming systems. That's why the C language is widely used for developing...
Ganesh
0 0
5
Why are C and C++ faster than other programming languages?
C is basic programming language. It is very simple language & interested. C is procedural programming language
Kalyani
0 0
8

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

C Program-Error Handling
//Header files #include<stdio.h>#include<conio.h>#include<stdlib.h> //Main function void main(){ int dividend=10; int divisor=0; int quotient; //Function for clearing screen clrscr(); ...

C Program to print Block Letter and Small Case Alphabets using C
/* WAP to print Block Letter and Small Case Alpahbets using C*/ //Hint:use ascii code(value) to print #include#include void main(){ int i; clrscr(); //Block Letters Alphabets printf("Block Letters Alphabets\n");...

Program to swap the value of two variables without using third variable(simplest way)...
int main() { int a=10,b=20; printf("values of a before swap ="%d,a); printf("values of b before swap ="%d,b); a=a+b; b=a-b; a=a-b; printf("******************"); printf("values of a after swap...

Datatypes in C Language
Data types in C Language Data types specify how we enter data into our programs and what type of data we enter. C language has some predefined set of data types to handle various kinds of data that...

Programing Languages Learning Tricks
You want to learn that new language or library or framework as soon as possible, right? That’s understandable. Fortunately, there are a handful of tips that can help you to better retain all of that...

Harshal G.

0 0
0

Recommended Articles

Brilliant Academy is one of the reputed institutes for B.Tech tuition classes. This institute is specialised in delivering quality tuition classes for B.E, Engineering - all streams and Engineering diploma courses. Incorporated in 2012, Brillant Academy is a brainchild of Mr Jagadeesh. The main motto of the academy is to...

Read full article >

Lasya Infotech is a Hyderabad based IT training institute founded in 2016 by O Venkat. Believing in his innovation, passion and persistence and with a diverse blend of experience, he started his brainchild to deliver exemplary professional courses to aspiring candidates by honing their skills. Ever since the institute envisions...

Read full article >

Microsoft Excel is an electronic spreadsheet tool which is commonly used for financial and statistical data processing. It has been developed by Microsoft and forms a major component of the widely used Microsoft Office. From individual users to the top IT companies, Excel is used worldwide. Excel is one of the most important...

Read full article >

Whether it was the Internet Era of 90s or the Big Data Era of today, Information Technology (IT) has given birth to several lucrative career options for many. Though there will not be a “significant" increase in demand for IT professionals in 2014 as compared to 2013, a “steady” demand for IT professionals is rest assured...

Read full article >

Looking for C Language Classes?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for C Language Classes?

The best tutors for C Language Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C Language with the Best Tutors

The best Tutors for C Language Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more