Thread: how to swap int or char using marcos

  1. #1
    kotin
    Join Date
    Oct 2009
    Posts
    132

    how to swap int or char using marcos

    Hi friends,

    how i can swap two char or int using macros statement function?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    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

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.)

  5. #5
    kotin
    Join Date
    Oct 2009
    Posts
    132
    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

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  7. #7
    kotin
    Join Date
    Oct 2009
    Posts
    132
    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

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  9. #9
    kotin
    Join Date
    Oct 2009
    Posts
    132
    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

  10. #10
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    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

  11. #11
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Because one programer asked me this question. if any one get idea ,please post .


    i look for your further replys...

  12. #12
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    my head is spinnin and i think it can't be done the only way i can right now is function template
    not macro

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Considering a char will fit in an int, yes you could.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by nkrao123@gmail. View Post
    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
    Your swap macro will swap ints and chars. You could have figured that out yourself, had you bothered to try it.

  15. #15
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    work around pal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  2. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM