+ 1
What is the function of variable temp
5 odpowiedzi
+ 2
hi,
temp car used to store the value of var temporarily.
but this temp will consume some resources and u can avoid to use this temp.
with using temp var to swap two numbers
********************************************
swap=changing the value of two variables.
temp=a;
a=b;
b=temp;
here value of a and b is 5 and 10
then after executing the above code the value of a and b is 10 and 5 respectively.
without using temp variable
*****************************
a=a+b;
b=a-b;
a=a-b;
by executing the above code will also swap the variables
+ 2
interchanging like i have done put the value of b in a and vice versa
+ 2
swapping means interchanging the values of 2 variable
eg:
int a=1,b=2,temp;
a=temp;
b=a;
a=temp;
The above code will swap the values in a and b
then the value a will be 2 and b will be 1
+ 1
it is generally used when we need temporary variables like for swapping two numbers.
temp=a;
a=b;
b=temp;
0
swapping means