Skip to content

Instantly share code, notes, and snippets.

@icyflame
Last active July 13, 2017 06:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icyflame/5d0cc83b8a81c50a466f67dfd5110826 to your computer and use it in GitHub Desktop.
Save icyflame/5d0cc83b8a81c50a466f67dfd5110826 to your computer and use it in GitHub Desktop.
A cheat sheet for some of the common c code that's often used

#define

#define iterate(i, n) for (i = 0; i < n; ++i)
#define print_int_arr(arr, i, n) for (i = 0; i < n; ++i) printf("%d, ", arr[i]);

qsort

int cmp (const void *a, const void *b) {
  return (*(const int *)a > *(const int *)b) - (*(const int *)a < *(const int *)b);
}
int *ptr = malloc(sizeof(int) * count);
qsort(ptr, count, sizeof(int), cmp);

sprintf

char *test = malloc(sizeof(char) * 10);
int year = 2018;
sprintf(test, "12.01.%d", year);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment