Skip to content

Instantly share code, notes, and snippets.

@gdetor
Created February 7, 2023 00:42
Show Gist options
  • Save gdetor/b17c7f59e95ac0991f68c263849088a6 to your computer and use it in GitHub Desktop.
Save gdetor/b17c7f59e95ac0991f68c263849088a6 to your computer and use it in GitHub Desktop.
Create generic type functions in C
#include <stdio.h>
#define GENERIC_MAX( type ) \
type type##_MAX(type a, type b) \
{ \
return (a > b) ? a : b; \
}
int main()
{
int ia = 5, ib = 3;
float fa = 3.5, fb = 8.5;
GENERIC_MAX(int);
printf("%d \n", int_MAX(ia, ib));
GENERIC_MAX(float);
printf("%f \n", float_MAX(fa, fb));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment