#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);
}
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);
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
write a program that reads a phrase and prints the number of lowercase latters in it using a function for counting? in C program
Use list assignment i.e. for two variables $a, $b: ($a,$b) = ($b,$a)
This is not a question.
There is no gotoxy statement in C.
write a vb program to find the magic square
write a program that reads a phrase and prints the number of lowercase latters in it using a function for counting? in C program
To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;
#include<
a=a^b; b=a^b; a=a^b;
Use list assignment i.e. for two variables $a, $b: ($a,$b) = ($b,$a)
not possible dude
This is not a question.
give an example of calculation of mathematics
dim a input a
There will be a function in it like this: double RectangleArea (double a, double b) { return a*b; }
Into the source program.
Use text-editor notepad++