Thread: loop with #define

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    124

    loop with #define

    Can someone explain to me why this doesn't work or breaks during compiled time.

    Code:
    #define now_inc ( x ) ++(x);  now_loop ( (x) )
    #define now_dec ( x ) --(x); now_loop ( (x) )
    #define now_loop( x ) (x) < 5 ? now_inc( (x) ) : now_dec( (x) )
    
    int main( void )
    {
     int i = 0;
     now_loop( i )
     printf("%d", i );
     return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You're missing a semicolon somewhere.
    Kurt

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Perhaps you need a different hobby.
    Code:
    $ gcc -E foo.c
    # 1 "foo.c"
    # 1 "<built-in>"
    # 1 "<command-line>"
    # 1 "foo.c"
    
    
    
    
    int main( void )
    {
     int i = 0;
     (i) < 5 ? ( x ) ++(x); now_loop ( (x) )( (i) ) : ( x ) --(x); now_loop ( (x) )( (i) )
     printf("%d", i );
     return 0;
    }
    Trying to rewrite the language using the C pre-processor is one of those way off esoteric things that you never see in practice.

    FWIW, you have a recursive expansion.
    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.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    124
    not sure if you're being rude or sarcastic, but I wasn't looking for any practical use for this attempt. I did look up that compilers just replaces x with whatever value, but not sure why i didn't replace x through the entire recursion. Actually, I did have a purpose. I know you can do recursive templates, and I wanted to see if there was a C-equivalent to doing a loop during compilation or prior. I guess not.
    Last edited by Darkinyuasha1; 04-05-2013 at 01:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. difference between #define and #define ()
    By bored_guy in forum C Programming
    Replies: 8
    Last Post: 07-20-2009, 06:58 PM
  2. Define Please
    By bumfluff in forum C++ Programming
    Replies: 4
    Last Post: 04-14-2006, 11:28 PM
  3. #define with a dot?
    By chunlee in forum C Programming
    Replies: 6
    Last Post: 11-08-2004, 10:49 AM
  4. I NEED #define HELP!!!!
    By Game_Addict27 in forum C++ Programming
    Replies: 18
    Last Post: 08-11-2004, 11:56 AM
  5. Infinite #define loop i can't find a way around
    By IonBlade in forum C++ Programming
    Replies: 6
    Last Post: 12-18-2003, 09:38 PM