Skip to content

Instantly share code, notes, and snippets.

@cheery
Created April 12, 2018 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cheery/a6cee9ecbb2cede36b2c5ccb3a414e22 to your computer and use it in GitHub Desktop.
Save cheery/a6cee9ecbb2cede36b2c5ccb3a414e22 to your computer and use it in GitHub Desktop.
A small demo on using C macros
# 1 "symtab_demo.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "symtab_demo.c"
static symtab symbol_table[] = {
{"add", fn_add},
{"print", fn_print},
{"sub", fn_sub},
{NULL, NULL}
};
/* This is a fairly useful feature to know if
you are writing anything more complicated in C.
Run gcc -E tiny_symtab.c to get the result.
*/
#define ENTRY(a) {#a, fn_##a}
static symtab symbol_table[] = {
ENTRY(add),
ENTRY(print),
ENTRY(sub),
{NULL, NULL}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment