Advertisement
Guest User

Recursive macros

a guest
Jan 15th, 2013
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.76 KB | None | 0 0
  1. //
  2. //  Factorial.h
  3. //  Sort
  4. //
  5. //  Created by me on 1/15/13.
  6. //  Copyright (c) 2013 me. All rights reserved.
  7. //
  8.  
  9. #ifndef Sort_Factorial_h
  10. #define Sort_Factorial_h
  11.  
  12. #define FACTORIAL(__TYPE__, n) _factorial_##__TYPE__(n)
  13.  
  14. #define CREATE_FACTORIAL_FUNC_WITH_TYPE(__TYPE__) \
  15.     static inline __TYPE__ _factorial_##__TYPE__(__TYPE__ n) { \
  16.             return ((((n) == 0))?1:((n) * FACTORIAL(__TYPE__, ((n) - 1)))); \
  17.     }
  18.  
  19. #endif
  20.  
  21. //
  22. //  main.c
  23. //  Sort
  24. //
  25. //  Created by me on 1/14/13.
  26. //  Copyright (c) 2013 me. All rights reserved.
  27. //
  28.  
  29. #include <stdio.h>
  30.  
  31. #include "Factorial.h"
  32.  
  33. CREATE_FACTORIAL_FUNC_WITH_TYPE(long);
  34.  
  35. int main(int argc, const char * argv[]) {
  36.     long result = FACTORIAL(long, 5);
  37.     printf("%ld\n", result);
  38.    
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement