94_generic.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <stdio.h>
  2. const int a = 0;
  3. struct a {
  4. int a;
  5. };
  6. struct b {
  7. int a;
  8. };
  9. int a_f()
  10. {
  11. return 20;
  12. }
  13. int b_f()
  14. {
  15. return 10;
  16. }
  17. typedef int int_type1;
  18. #define gen_sw(a) _Generic(a, const char *: 1, default: 8, int: 123);
  19. int main()
  20. {
  21. int i = 0;
  22. signed long int l = 2;
  23. struct b titi;
  24. const int * const ptr;
  25. const char *ti;
  26. int_type1 i2;
  27. i = _Generic(a, int: a_f, const int: b_f)();
  28. printf("%d\n", i);
  29. i = _Generic(a, int: a_f() / 2, const int: b_f() / 2);
  30. printf("%d\n", i);
  31. i = _Generic(ptr, int *:1, int * const:2, default:20);
  32. printf("%d\n", i);
  33. i = gen_sw(a);
  34. printf("%d\n", i);
  35. i = _Generic(titi, struct a:1, struct b:2, default:20);
  36. printf("%d\n", i);
  37. i = _Generic(i2, char: 1, int : 0);
  38. printf("%d\n", i);
  39. i = _Generic(a, char:1, int[4]:2, default:5);
  40. printf("%d\n", i);
  41. i = _Generic(17, int :1, int **:2);
  42. printf("%d\n", i);
  43. i = _Generic(17L, int :1, long :2, long long : 3);
  44. printf("%d\n", i);
  45. i = _Generic("17, io", char *: 3, const char *: 1);
  46. printf("%d\n", i);
  47. i = _Generic(ti, const unsigned char *:1, const char *:4, char *:3,
  48. const signed char *:2);
  49. printf("%d\n", i);
  50. printf("%s\n", _Generic(i + 2L, long: "long", int: "int",
  51. long long: "long long"));
  52. i = _Generic(l, long: 1, int: 2);
  53. printf("%d\n", i);
  54. return 0;
  55. }