changeset 3:221b3052e3b8

add c_example
author ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
date Fri, 12 Mar 2021 19:21:15 +0900
parents 3a1b47368e51
children d6911fe00127
files c_example/macro.c
diffstat 1 files changed, 33 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/c_example/macro.c	Fri Mar 12 19:21:15 2021 +0900
@@ -0,0 +1,33 @@
+#include <stdio.h>
+
+#define HOGE ("hoge")
+#define ADD(a, b) (a+b)
+#define FALSE (0)
+#define TRUE (1)
+
+#define FOO(a, b) (printf("%d\n",a->b.foo))
+//% $CBC_COMPILER -E c_example/macro.c  | less で見比べてみよう。
+//macroとしてFOOを定義している。コンパイル直前で置換される、
+
+typedef struct hoge {
+  int foo;
+  char* bar;
+} hoge;
+
+typedef struct hoge2 {
+  int foo;
+  char* bar;
+} hoge2;
+
+union UNION{
+  struct hoge hoge;
+  struct hoge2 hoge2;
+};
+
+int main(){
+  hoge hu = {11, "hello"};
+  union UNION *unionTest = (union UNION*) &hu;
+  FOO(unionTest, hoge);
+  puts(HOGE);
+  return FALSE;
+}