Skip to content

Instantly share code, notes, and snippets.

@patrickbucher
Created October 11, 2018 14:20
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 patrickbucher/50e5b8e7b0e95f981bc4f700109342c3 to your computer and use it in GitHub Desktop.
Save patrickbucher/50e5b8e7b0e95f981bc4f700109342c3 to your computer and use it in GitHub Desktop.
Why C Function Macros Are Bad
#include <stdio.h>
#define IS_DIGIT(C) ((C) >= '0' && (C) <= '9')
int main()
{
int i = 0, n = 0;
char *s = "a!b+c(75"; // two digits, right?
while (s[i] != '\0') {
if (IS_DIGIT(s[i++])) {
n++;
}
}
printf("%d digits found in '%s'\n", n, s); // guess what!
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment