alansam

Stupid macro tricks.

Jul 7th, 2020
1,281
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $ cat hw058.c
  2.  
  3. #include <stdio.h>
  4.  
  5. inline
  6. static
  7. int p(int x) {
  8.   return x * x;
  9. }
  10.  
  11. #define P(X) (X * X)
  12. #define Q(X) ((X) * (X))
  13.  
  14. int main(int argc, char const * argv[]) {
  15.  
  16.   int i = 3;
  17.   printf("%d %d ", i, P(i + 1));
  18.   printf("%d %d ", i, P(i++));
  19.   printf("%d %d ", i, P(++i));
  20.   printf("%d ", i);
  21.   putchar('\n');
  22.  
  23.   i = 3;
  24.   printf("%d %d ", i, Q(i + 1));
  25.   printf("%d %d ", i, Q(i++));
  26.   printf("%d %d ", i, Q(++i));
  27.   printf("%d ", i);
  28.   putchar('\n');
  29.  
  30.   i = 3;
  31.   printf("%d %d ", i, p(i + 1));
  32.   printf("%d %d ", i, p(i++));
  33.   printf("%d %d ", i, p(++i));
  34.   printf("%d ", i);
  35.   putchar('\n');
  36.  
  37.   return 0;
  38. }
  39.  
  40. # Compiling with clang
  41. # ##################################################################################
  42. $ clang -Wall -std=gnu18 -o ../../bin/hw058c hw058.c
  43. hw058.c:17:26: warning: unsequenced modification and access to 'i' [-Wunsequenced]
  44.   printf("%d %d ", i, P(i++));
  45.                    ~     ^
  46. hw058.c:10:15: note: expanded from macro 'P'
  47. #define P(X) (X * X)
  48.               ^
  49. hw058.c:18:25: warning: unsequenced modification and access to 'i' [-Wunsequenced]
  50.   printf("%d %d ", i, P(++i));
  51.                    ~    ^
  52. hw058.c:10:15: note: expanded from macro 'P'
  53. #define P(X) (X * X)
  54.               ^
  55. hw058.c:24:26: warning: unsequenced modification and access to 'i' [-Wunsequenced]
  56.   printf("%d %d ", i, Q(i++));
  57.                    ~     ^
  58. hw058.c:11:16: note: expanded from macro 'Q'
  59. #define Q(X) ((X) * (X))
  60.                ^
  61. hw058.c:25:25: warning: unsequenced modification and access to 'i' [-Wunsequenced]
  62.   printf("%d %d ", i, Q(++i));
  63.                    ~    ^
  64. hw058.c:11:16: note: expanded from macro 'Q'
  65. #define Q(X) ((X) * (X))
  66.                ^
  67. hw058.c:31:26: warning: unsequenced modification and access to 'i' [-Wunsequenced]
  68.   printf("%d %d ", i, p(i++));
  69.                    ~     ^
  70. hw058.c:32:25: warning: unsequenced modification and access to 'i' [-Wunsequenced]
  71.   printf("%d %d ", i, p(++i));
  72.                    ~    ^
  73. 6 warnings generated.
  74.  
  75. # Compiling with gcc
  76. # ##################################################################################
  77. $ gcc-9 -Wall -std=gnu18 -o ../../bin/hw058g hw058.c
  78. hw058.c: In function 'main':
  79. hw058.c:17:26: warning: operation on 'i' may be undefined [-Wsequence-point]
  80.    17 |   printf("%d %d ", i, P(i++));
  81.       |                         ~^~
  82. hw058.c:10:15: note: in definition of macro 'P'
  83.    10 | #define P(X) (X * X)
  84.       |               ^
  85. hw058.c:17:26: warning: operation on 'i' may be undefined [-Wsequence-point]
  86.    17 |   printf("%d %d ", i, P(i++));
  87.       |                         ~^~
  88. hw058.c:10:15: note: in definition of macro 'P'
  89.    10 | #define P(X) (X * X)
  90.       |               ^
  91. hw058.c:18:25: warning: operation on 'i' may be undefined [-Wsequence-point]
  92.    18 |   printf("%d %d ", i, P(++i));
  93.       |                         ^~~
  94. hw058.c:10:15: note: in definition of macro 'P'
  95.    10 | #define P(X) (X * X)
  96.       |               ^
  97. hw058.c:18:25: warning: operation on 'i' may be undefined [-Wsequence-point]
  98.    18 |   printf("%d %d ", i, P(++i));
  99.       |                         ^~~
  100. hw058.c:10:15: note: in definition of macro 'P'
  101.    10 | #define P(X) (X * X)
  102.       |               ^
  103. hw058.c:24:26: warning: operation on 'i' may be undefined [-Wsequence-point]
  104.    11 | #define Q(X) ((X) * (X))
  105.       |               ~~~        
  106. ......
  107.    24 |   printf("%d %d ", i, Q(i++));
  108. hw058.c:11:16: note: in definition of macro 'Q'
  109.    11 | #define Q(X) ((X) * (X))
  110.       |                ^
  111. hw058.c:24:26: warning: operation on 'i' may be undefined [-Wsequence-point]
  112.    11 | #define Q(X) ((X) * (X))
  113.       |               ~~~        
  114. ......
  115.    24 |   printf("%d %d ", i, Q(i++));
  116. hw058.c:11:16: note: in definition of macro 'Q'
  117.    11 | #define Q(X) ((X) * (X))
  118.       |                ^
  119. hw058.c:25:25: warning: operation on 'i' may be undefined [-Wsequence-point]
  120.    11 | #define Q(X) ((X) * (X))
  121.       |               ~~~        
  122. ......
  123.    25 |   printf("%d %d ", i, Q(++i));
  124. hw058.c:11:16: note: in definition of macro 'Q'
  125.    11 | #define Q(X) ((X) * (X))
  126.       |                ^
  127. hw058.c:25:25: warning: operation on 'i' may be undefined [-Wsequence-point]
  128.    11 | #define Q(X) ((X) * (X))
  129.       |               ~~~        
  130. ......
  131.    25 |   printf("%d %d ", i, Q(++i));
  132. hw058.c:11:16: note: in definition of macro 'Q'
  133.    11 | #define Q(X) ((X) * (X))
  134.       |                ^
  135. hw058.c:31:26: warning: operation on 'i' may be undefined [-Wsequence-point]
  136.    31 |   printf("%d %d ", i, p(i++));
  137.       |                         ~^~
  138. hw058.c:32:25: warning: operation on 'i' may be undefined [-Wsequence-point]
  139.    32 |   printf("%d %d ", i, p(++i));
  140.       |                         ^~~
  141.  
  142. # Running clang binary
  143. # ##################################################################################
  144. $ ../../bin/hw058c
  145. 3 7 3 12 5 42 7
  146. 3 16 3 12 5 42 7
  147. 3 16 3 9 4 25 5
  148. # Running gcc binary
  149. # ##################################################################################
  150. $ ../../bin/hw058g
  151. 3 7 5 12 7 49 7
  152. 3 16 5 12 7 49 7
  153. 3 16 4 9 5 25 5
RAW Paste Data