Created
October 11, 2018 14:20
-
-
Save patrickbucher/50e5b8e7b0e95f981bc4f700109342c3 to your computer and use it in GitHub Desktop.
Why C Function Macros Are Bad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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