Hi friends,
how i can swap two char or int using macros statement function?
Hi friends,
how i can swap two char or int using macros statement function?
Instead of making this post, you could have just spent the same amount of time using the search function, or any search engine for that matter, and had your answer already.
Quzah.
Hope is the first step on the road to disappointment.
Rao
macro is a pre processor directive and macro will expand at compile time if u want to see how compiler and how it is expending then u can use -E option with gcc
i can give u hint
Code:#define SWAP(a, b) { \ int temp = a; \ a = b; \ b = temp; \ }
And try to do it first urself very hard and if u find any problem then come here it will be good for ur learning
You're going to have to decide whether you want to use a macro, or a statement, or a function. (Actually you can't use a single statement, since it requires three statements to swap two values.)
Hi friends,
i am trying for same macro function swap for both char and int values. i writen two functions for swap
Code:#include<stdio.h> #define swap(x,y) int t;t=x;x=y;y=t; #define swapc(xc,yc) char p;p=xc;xc=yc;yc=p; int main() { int a=10,b=20; char c1='a',c2='b'; swap(a,b); printf("a=%d b=%d \n",a,b); swapc(c1,c2); printf("c1=%c c2=%c \n",c1,c2); return 0; }
i am trying to use swap(c1,c2) function also swap(x,y) inplace of swapc(xc,yc) .
any body can help me
Then do so. (I.e. type swap(c1,c2) and be done with it.) The only issue here is that if you try to use this macro more than once, you will have a double definition of your temporary variable. You may want to add extra curly braces so that the scope rules will work in your favor.
actuvaly i used two macros to one for int and one for swap. my intension is i minimise my code to one macro for both int and char.
please give suggestions for to use one macro for both int and char values to swap.
please any body send me that macro function
What's the reasoning behind you using a macro? Just to save typing?Code:#define swap(a,b,c) (c)=(a);(a)=(b);(b)=(c) ... int temp, a=5, b=6; ... swap(a,b,temp);
Quzah.
Hope is the first step on the road to disappointment.
actuvaly any body swap using functions , but i am trying to swap input is char or int using one macro function.
till now i didt get that logic
with one macro function i think it will not be possible to swap char and int what u can do i think have swap template function then any type of data type can be swapped
Because one programer asked me this question. if any one get idea ,please post .
i look for your further replys...
my head is spinnin and i think it can't be done the only way i can right now is function template
not macro
Considering a char will fit in an int, yes you could.
Quzah.
Hope is the first step on the road to disappointment.
work around pal