summaryrefslogtreecommitdiffstats
path: root/4/14.c
blob: f1422577b5688b84d61d3192def97d33ce3313df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>

#define swap(t, x, y)                                                          \
  {                                                                            \
    t z = x;                                                                   \
    x = y;                                                                     \
    y = z;                                                                     \
  }

/* Swaps two arguments using a macro */
int main(int argc, char *argv[]) {
  int a, b;
  double x, y;
  char *s1, *s2;

  a = 1;
  b = 2;
  swap(int, a, b);
  printf("\ta=%d, b=%d\n", a, b);

  x = 3.0;
  y = 5.5;
  swap(double, x, y);
  printf("\tx=%f, y=%f\n", x, y);

  s1 = "hello";
  s2 = "world";
  swap(char *, s1, s2);
  printf("\ts1=%s, s2=%s\n", s1, s2);

  return 0;
}