[C] Macro's Are Beautiful

AceInfinity

Emeritus, Contributor
Joined
Feb 21, 2012
Posts
1,728
Location
Canada
A bit of code I wrote in which demonstrates a few reasons why macro's can be a beautiful thing. :)

Code:
[NO-PARSE]#include <stdio.h>

#define GCC_DO_PRAGMA(x) _Pragma(#x)
#define    GCC_IGNORE(x) GCC_DO_PRAGMA(GCC diagnostic ignored x)

#define PRAGMA_MESSAGE(x, y) GCC_DO_PRAGMA(message(#x": "#y))
#define NOTE(x) PRAGMA_MESSAGE(NOTE, x)
#define TODO(x) PRAGMA_MESSAGE(TODO, x)
#define  BUG(x) PRAGMA_MESSAGE(BUG, x)


#if defined( _WIN64 )
  /* Windows 64 bit code here */
#elif defined( _WIN32 )
  /* Windows 32 bit code here */
#elif defined(UNIX)
  /* Unix specific code */
#else
  #error "Unknown operating system?"
#endif

#define def_find_max(T)         \
  T find_max_##T(T x, T y, T z) \
  {                             \
    T tmp = x > y ? x : y;      \
    return z > tmp ? z : tmp;   \
  }
#define find_max(T) find_max_##T

def_find_max(int)
def_find_max(double)
def_find_max(float)

GCC_IGNORE("-Wunused-but-set-variable")

int main(void)
{
  int set_but_never_used; set_but_never_used = -1;
  int x = 10,
      y = 15,
      z = 5;
  printf("%d\n", find_max(int)(x, y, z));
  printf("%.1f\n", find_max(double)(2.0, 3.5, 5.0));
  printf("%.1f\n", find_max_float(0.1f, 0.3f, 0.2f));
}[/NO-PARSE]

:thumbsup2:
 
Last edited:

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top