Skip to content

Instantly share code, notes, and snippets.

@hidsh
Last active July 30, 2025 00:22
Show Gist options
  • Save hidsh/78d44feb175fa49b5afeea0139c20b1d to your computer and use it in GitHub Desktop.
Save hidsh/78d44feb175fa49b5afeea0139c20b1d to your computer and use it in GitHub Desktop.
c example to override functions using `#define` macros
#include <stdio.h>
int fun0(void) { return 5; }
int fun1(int x) { return x * 2; }
#define OVERRIDE 1
#if OVERRIDE == 1
#define fun0() (9)
#define fun1(x) (99)
// To override functions, these `#define funXX` macros
// must be placed *after* the function definitions overridden
// regardless of their prototype declarations or not.
#endif
int main(void)
{
printf("OVERRIDE = %d: fun0 => %d, fun1 => %d\n",
OVERRIDE, fun0(), fun1(1));
return 0;
}
/*
OVERRIDE = 0: fun0 => 5, fun1 => 2
OVERRIDE = 1: fun0 => 9, fun1 => 99
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment