Skip to content

Instantly share code, notes, and snippets.

@gjlmotea
Created July 12, 2020 07:24
Show Gist options
  • Save gjlmotea/6bb3069b54a687d7f935bb7e58cb583b to your computer and use it in GitHub Desktop.
Save gjlmotea/6bb3069b54a687d7f935bb7e58cb583b to your computer and use it in GitHub Desktop.
C macro陷阱
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
#define PI 3.14159
#define Square(x) x*x
#define Square_(x) x*(x)
#define Square__(x) ((x)*(x))
printf("%f\n", PI);
printf("\n");
printf("%d\n", Square(5));
printf("%d\n", Square(2+3));
printf("\n");
printf("%d\n", Square_(5));
printf("%d\n", Square_(2+3));
printf("\n");
printf("%d\n", Square__(5));
printf("%d\n", Square__(2+3));
printf("\n");
int i;
i = 4;
printf("%d\n", Square__(i++));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment