answersLogoWhite

0


Best Answer

#include<iostream>

void swap(int* x, int* x){

(*x)^=(*y)^=(*x)^=(*y);

}

int main()

{

int a, b;

a=10;

b=20;

std::cout<<"Before swap: a="<<a<<", b="<<b<<std::endl;

swap(&a,&b);

std::cout<<"After swap: a="<<a<<", b="<<b<<std::endl;

return(0);

}

User Avatar

Wiki User

10y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

To swap two variable using a function in C or C++, you need to pass the addresses, not the values, of the variables to the function.

swap (int *i, int *j) {

*i ^= *j;

*j ^= *i;

*i ^= *j;

}

example of call

int a = 12;

int b = 17;

swap (&a, &b);

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

C does not support move semantics thus in order to swap values we must copy them using a temporary variable.

int a = 42, b = 24;

// ...

// swap the values of a and b

int temp = a; a = b;

b = temp;

// a = 24, b = 42

Given the general usefulness of swapping, it makes sense to define a function. However, C only supports the pass by value semantic, so if we pass the values, only the formal argument values will be swapped, not the values of the actual arguments we passed. In order to swap the actual arguments we must pass their addresses (by value). This is also known as pass by reference even though C has no native reference type (C++ does have a native reference type).

void swap (int* a, int* b) {

int temp = *a;

*a = *b;

*b = temp;

}

Usage:

int x = 42, y = 24;

swap (&x, &y); // pass by reference

// x = 24, y = 42

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a program in C to swap two variables using a function?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a program that read phrase and print the number of lower-case letter in it using function of counting?

write a program that reads a phrase and prints the number of lowercase latters in it using a function for counting? in C program


How do you write a program in C plus plus plus plus How do you write a program in C to swap two variables without using the third oneo swap two variables without using the third one?

To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;


Write a c program for matrix addition using function?

#include&lt;


How do you write a program in C to swap two variables without using the third one using XOR?

a=a^b; b=a^b; a=a^b;


How do you write a program in Perl to swap two variables without using the third one?

Use list assignment i.e. for two variables $a, $b: ($a,$b) = ($b,$a)


Can you write a program without using any semicolon inside the main function?

not possible dude


Write a Program to convert decimal number into hexadecimal no without using function and string?

This is not a question.


Write a program to calculate the area of a cylinder using a function?

give an example of calculation of mathematics


How do you write BASIC program to accept variables?

dim a input a


How do you write a c program to find area of rectangle using function?

There will be a function in it like this: double RectangleArea (double a, double b) { return a*b; }


Where do we write main function in a c program?

Into the source program.


How do you write a program using function that counts the number of vowels in a string in java language?

Use text-editor notepad++