small prob

This is a discussion on small prob within the C++ Programming forums, part of the General Programming Boards category; hi, ineed to know what is wrong with this macro definition #define fac(a) (a)*fac((a)-1)...

  1. #1
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    small prob

    hi,
    ineed to know what is wrong with this macro definition

    #define fac(a) (a)*fac((a)-1)
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    You cannot put recursion in a define. What the compiler does is run through the complete code and replace all defines.

    So fac(10) is replaced by: (10)*fac((10)-1)

    Remember that this is compiletime, not runtime, so the compiler does nothing with the value 10.

    After this you will get a link error because the fac function cannot be found.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Using macros in C++ is far from perfect. I reckon you should consider using inline functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. got prob with this small program
    By Hugo716 in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2006, 08:46 PM
  2. want to make this small program...
    By psycho88 in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2005, 01:05 AM
  3. Small array of strings prob
    By bstringer in forum C Programming
    Replies: 15
    Last Post: 08-04-2005, 06:27 PM
  4. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM
  5. yhatzee, small straight
    By uglyjack in forum C++ Programming
    Replies: 2
    Last Post: 06-13-2002, 03:09 AM

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