answersLogoWhite

0


Best Answer

You can swap values a and b without a temporary as follows:

a=a^b

b=a^b

a=a^b

(^ is the bitwise XOR operator)

To show how this works, let's consider the values a=180 and b=204. In binary, 180 is 10110100 while 204 is 11001100. Thus:

a = 10110100

b = 11001100

a^b means that we look at the corresponding bits in each input value and output a 1 if one and only one of the two input bits is set. Thus the output (o) is:

a = 10110100

b = 11001100

o = 01111000

In the expression a=a^b, the output of a^b is assigned to a, thus the values of a and b now look like this:

a = 01111000

b = 11001100

We then repeat the operation, a^b, this time assigning the output to b:

a = 01111000

b = 11001100

o = 10110100

So now we have:

a = 01111000

b = 10110100

We repeat the operation one more time, assigning the output value to a:

a = 01111000

b = 10110100

0 = 11001100

So now we have:

a = 11001100

b = 10110100

The two values have now been swapped.

To write this in C, we can use shorthand expressions using the compound XOR-ASSIGN operator (^=):

a^=b

b^=a

a^=b

These individual expressions can then be combined into a single expression:

a^=b^=a^=b

Finally, to use this expression in a C function, we need to pass a and b by reference (using pointer variables):

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

(*a)^=(*b)^=(*a)^=(*b);

}

Note that a and b must be of an integral type (char, short, long, int, unsigned, etc). The example above only works on type int. If we wish to swap other types, or more complex types, we must treat those values as if they were an integral type. One way to achieve this is to treat those types as if they were an array of type char, supplying the length of the arrays through a separate argument:

void swap (char* a, char* b, size_t size) {

for (int i=0; i<size; ++i) {

a[i]^=b[i]^=a[i]^=b[i];

}

}

For example, to swap two doubles, we can use the following call:

int main (void) {

double a, b;

a = 3.14;

b = 1.1;

swap ((char*) &a, (char*) &b, sizeof (double));

assert (a==1.1);

assert (b==3.14);

return 0;

}

Note that a and b must of the same type.

User Avatar

Wiki User

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

Wiki User

14y ago

char a,b; //suppose these two are character variable

//assigning different values to each
a='d';
b='f';

char temp; //temp for holding the value of one of the character

temp=a; //a's value stored in temp
a=b; //a's new value is the value in b
b=temp; //b takes the a's old value which is stored in temp

//operation done

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a c function to swap two variables without using a temporary variable?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the Algorithm for exchanging values of two variables?

There are three primary algorithms to exchange the values of two variables. Exchange with Temporary Variable temp = a; a = b; b = temp; Exchange Without Temporary Variable Using Exclusive Or a = a ^ b; b = b ^ a; a = a ^ b; Exchange Without Temporary Variable Using Arithmetic a = a + b; b = b - a; a = a - b;


Why static variables in a function have global extent?

A variable declared static outside of a function has a scope the of the source file only. It is not a global variable. A variable declared outside of a function and without the static qualifier would be a global variable.


What is a variable that is used within a function?

If the variable is declared within the function body, it is a local variable, one that is local to the function. Local variables fall from scope when the function returns, they are only accessible within the function. However, local variables can be returned by value, which creates an automatic variable that is returned to the caller. If the caller does not store the return value, the automatic variable falls from scope when the expression containing the function call ends. However, the expression may evaluate the return value without storing it. Note that functions cannot return local variables by reference since the local variable falls from scope when the function returns. If the variable is passed as an argument to the function, then the variable is a parameter of the function. Arguments may be passed by value or by reference, depending upon the function signature. Passing by value means the function parameter is a copy of the argument (if the argument is an object, the object's copy constructor is invoked automatically). Thus any changes made to the parameter within the function are not reflected in the argument that was originally passed, and the parameter will fall from scope when the function returns. However, the value of the parameter can be returned as previously explained. Passing by reference means the function parameter refers directly to the argument that was passed. Thus any changes made to the parameter are reflected in the argument. Parameters that are declared as constant references assure the caller that the reference's immutable members will not be altered by the function. If the parameter is a non-const reference but the caller does not wish changes to be reflected in the argument, the caller should pass a copy of the argument instead.


What is global variable in C programming?

A global variable is a variable that is declared at global scope, rather than file, namespace, function, class or nested scope. Global variables are usually declared with external linkage within a header and initialised in one (and only one) source file. Any file that includes the header (which includes the source file that initialised the global variable) then has unrestricted access to the variable. It is globally visible and any code can alter it. Global variables should be used sparingly and only when absolutely necessary. If the vast majority of the functions in your program require access to a particular variable, then a global variable makes perfect sense and is by far the simplest solution. However, a variable that is only used by a handful of functions can hardly be described as a global entity, thus it has no place within the global namespace and should be scoped to those functions that actually require it instead.


What facility static gives in class?

Static variables or function provides global view to the application that is u can access a static variable with one or more than one objects of a single class without loosing i.e. reinitializing its value.

Related questions

What is the Algorithm for exchanging values of two variables?

There are three primary algorithms to exchange the values of two variables. Exchange with Temporary Variable temp = a; a = b; b = temp; Exchange Without Temporary Variable Using Exclusive Or a = a ^ b; b = b ^ a; a = a ^ b; Exchange Without Temporary Variable Using Arithmetic a = a + b; b = b - a; a = a - b;


Why static variables in a function have global extent?

A variable declared static outside of a function has a scope the of the source file only. It is not a global variable. A variable declared outside of a function and without the static qualifier would be a global variable.


How do you calculate Total Cost without Total variable cost?

To calculate the Total Cost without Total variable cost, one should estimate for the variables or substitute for the variables with a variable such as X or Y and then solve for the approximate total cost.


What is the advantage of swapping two variables without using 3 variable?

Nothing. Never do that.


What is -15x-3y equals 9?

Since there are two variables in this sum, without knowing what one of the variables is, it is impossible to calculate the other variable.


What is the difference between controlling variables and using controls?

Controlling variables is when you make sure that only one variable is being tested at a time and that there are not other variables that will make your results unclear. Using a control is when you do a trial without the variable to see what the normal results are.


What is a variable that is used within a function?

If the variable is declared within the function body, it is a local variable, one that is local to the function. Local variables fall from scope when the function returns, they are only accessible within the function. However, local variables can be returned by value, which creates an automatic variable that is returned to the caller. If the caller does not store the return value, the automatic variable falls from scope when the expression containing the function call ends. However, the expression may evaluate the return value without storing it. Note that functions cannot return local variables by reference since the local variable falls from scope when the function returns. If the variable is passed as an argument to the function, then the variable is a parameter of the function. Arguments may be passed by value or by reference, depending upon the function signature. Passing by value means the function parameter is a copy of the argument (if the argument is an object, the object's copy constructor is invoked automatically). Thus any changes made to the parameter within the function are not reflected in the argument that was originally passed, and the parameter will fall from scope when the function returns. However, the value of the parameter can be returned as previously explained. Passing by reference means the function parameter refers directly to the argument that was passed. Thus any changes made to the parameter are reflected in the argument. Parameters that are declared as constant references assure the caller that the reference's immutable members will not be altered by the function. If the parameter is a non-const reference but the caller does not wish changes to be reflected in the argument, the caller should pass a copy of the argument instead.


What is global variable in C programming?

A global variable is a variable that is declared at global scope, rather than file, namespace, function, class or nested scope. Global variables are usually declared with external linkage within a header and initialised in one (and only one) source file. Any file that includes the header (which includes the source file that initialised the global variable) then has unrestricted access to the variable. It is globally visible and any code can alter it. Global variables should be used sparingly and only when absolutely necessary. If the vast majority of the functions in your program require access to a particular variable, then a global variable makes perfect sense and is by far the simplest solution. However, a variable that is only used by a handful of functions can hardly be described as a global entity, thus it has no place within the global namespace and should be scoped to those functions that actually require it instead.


What is pointer and where it is used?

A pointer is a variable used to store a memory address. That is, we can use a pointer variable to refer to another variable (including other pointers). Although many variables within a program have names and can be referred to directly by those names, the majority of variables are anonymous; they have no names we can refer to. For instance, every variable declared on the heap is anonymous, every element of an array is anonymous and every node in a list is anonymous. Without pointers there would be no way to refer to these anonymous variables. Moreover, there would be no way to allocate objects on the heap without pointers. Pointers also make it possible to pass addresses into functions. In C, for instance, all arguments to a function are passed by value, however a pointer's value is a memory address thus pointers allow us to use pass by reference semantics. This makes it possible for a function to refer to an object outwith the local scope of the function.


What facility static gives in class?

Static variables or function provides global view to the application that is u can access a static variable with one or more than one objects of a single class without loosing i.e. reinitializing its value.


What are the variables for your science project?

There are two types of variables. The first one is called the experimental variable. It is what you are compare everything to or the normal thing. For instance, what plant grows better the one with sunlight or the one without. The one with sunlight would be the experimental variable. The second type of variable is the dependent variable, which is the data you are collecting. Relating back to the plant experiment, how well the plant grows would be the dependent variable.


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;