summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSadeep Madurange <smadurange@users.noreply.github.com>2021-12-26 20:18:14 +0800
committerSadeep Madurange <smadurange@users.noreply.github.com>2021-12-26 20:18:14 +0800
commit380e760eb23ea70d013726b2f3cbfa726c23aeb6 (patch)
treeb77129688c0c1d7ce14ce90bb2154c998a295889
parentb392ca9be3da36620092f33e055e59b2f8da6c54 (diff)
downloadk&r-exercises-380e760eb23ea70d013726b2f3cbfa726c23aeb6.tar.gz
4.14
-rw-r--r--4/14.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/4/14.c b/4/14.c
new file mode 100644
index 0000000..f142257
--- /dev/null
+++ b/4/14.c
@@ -0,0 +1,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;
+} \ No newline at end of file