そのプログラムです
#include <stdio.h>
#define swap(type, a, b) ( type c, c = a, (a = b), (b = c) )
int main(void)
{
int x,y;
printf("マクロ\n");
printf("xの数を入力してください"); scanf("%d",&x);
printf("yの数を入力してください"); scanf("%d",&y);
swap(int, x, y);
printf("x=%d, y=%d\n", x, y);
return 0;
}コンパイル失敗
practice.c: 関数 ‘main’ 内:
practice.c:13:2: エラー: expected ‘)’ before ‘c’
practice.c:13:17: エラー: expected expression before ‘;’ token
と出ています
そこで3つ質問があります
1つ目です
なぜエラーになったのでしょうか(どこがいけなかったのでしょうか)
2つ目です
#define swap(type, a, b) ( type c, c = a, (a = b), (b = c) )
これを
#define swap(type, a, b) { type c; c = a; (a = b); (b = c); }
と書き換えるとコンパイル時にエラーが出ません
なぜなのでしょうか?
3つ目です
#define alert(str) (putchar('\a'), puts(str))
これはコンパイルができるのに
同じ()を使った
#define swap(type, a, b) ( type c, c = a, (a = b), (b = c) )
はなぜエラーになるのでしょうか?
C言語の知識は
配列や関数ぐらいまではでき、
ポインタや構造体はわかりません。
よろしくおねがいします。