macros

This is a discussion on macros within the C Programming forums, part of the General Programming Boards category; You could avoid the entire temporary variable with the XOR method: Code: #include <stdio.h> #define swap(a,b) a^=b^=a^=b int main(void) { ...

  1. #16
    -AppearingOnThis..........
    Join Date
    May 2005
    Location
    Netherlands
    Posts
    44
    You could avoid the entire temporary variable with the XOR method:
    Code:
    #include <stdio.h>
    
    #define swap(a,b) a^=b^=a^=b
    
    int main(void)
    {
    	int i, j;
    	
    	i = 5;
    	j = 10;
    	
    	printf("i: %d, j: %d\n", i, j);
    	swap(i, j);
    	printf("i: %d, j: %d", i, j);
    	
    	return 0;
    }

  2. #17
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    Yeah, that too (or why it's a bad idea) is also in the clc FAQ
    http://www.eskimo.com/~scs/C-faq/faq.html
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #18
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    why does everyone uses the
    Code:
    do{...}while(0);
    stuff ???
    two simple brackets do just fine {...}

    Quote Originally Posted by SirNot
    Code:
    #define swap(a,b) a^=b^=a^=b
    That fails when a and b are equal.

    My way, unfortunatly C99 or C++
    Code:
    #define swap(a,b) {\
        char t[sizeof(a)];\
        memcpy(t,&a,sizeof(a));\
        memcpy(&a,&b,sizeof(a));\
        memcpy(&b,t,sizeof(a));\
    }
    good stuff about this is that it works for any type, including unions, structs or even C++ classes.
    Last edited by xErath; 06-12-2005 at 07:29 PM.

  4. #19
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Quote Originally Posted by xErath
    why does everyone uses the
    Code:
    do{...}while(0);
    stuff ???
    two simple brackets do just fine {...}
    I believe it's stated in the standard somewhere that you should use that. And actually, it's:

    Code:
    do{...}while(0)
    That way when someone using the macro uses it, they can tack on a semicolon at the end and be correct.

  5. #20
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >Nice, but can you spot the bug in Dave's solution?


    >why does everyone uses the
    >two simple brackets do just fine {...}
    Code:
    #include <stdio.h>
    
    #define why()  { puts("because"); }
    
    int main(void)
    {
       int i = 42;
       if ( i )
          why();
       else
          puts("oh");
       return 0;
    }
    Or, follow the link.

    >That fails when a and b are equal.

    It UB, pure and simple.
    Last edited by Dave_Sinkula; 06-12-2005 at 08:49 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #21
    Jez
    Jez is offline
    The C-er
    Join Date
    Mar 2004
    Posts
    192
    Going back to the OP's original question, (which I'm still not too clear about) GCC provides an operator called typeof() - would this help? I appreciate the non-portability implications.

  7. #22
    ---
    Join Date
    May 2004
    Posts
    1,379
    >>GCC provides an operator called typeof()

    It does? I could't find anything on it.

  8. #23
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Even if that operator exists, I wouldn't suggest using it. If you can't figure out a solution to your problem that works in most compilers, then you need to put your thinking cap back on.

  9. #24
    Jez
    Jez is offline
    The C-er
    Join Date
    Mar 2004
    Posts
    192
    >Even if that operator exists, I wouldn't suggest using it. If you can't figure out a solution to your problem that works in most compilers, then you need to put your thinking cap back on.

    I do agree. It looks like a nice feature, but I would never use it personally. A quick search on google shows that MS Visual C++ supports it too. Maybe one day it'll become part of the standard, then we can all use it.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Order of execution of preprocessor macros
    By DL1 in forum C Programming
    Replies: 2
    Last Post: 04-02-2009, 07:52 PM
  2. Pre-processor macros causing segfaults
    By nempo in forum C++ Programming
    Replies: 6
    Last Post: 02-10-2009, 02:35 AM
  3. Macros inside of macros
    By Chewie8 in forum C Programming
    Replies: 2
    Last Post: 02-24-2008, 03:51 AM
  4. Macros vs Inline Functions
    By vb.bajpai in forum C Programming
    Replies: 4
    Last Post: 08-02-2007, 12:51 PM
  5. template fn replacements for msg macros
    By Ken Fitlike in forum Windows Programming
    Replies: 17
    Last Post: 10-30-2002, 07:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21