C Programming - C Preprocessor - Discussion

Discussion Forum : C Preprocessor - General Questions (Q.No. 1)
1.
What will the SWAP macro in the following program be expanded to on preprocessing? will the code compile?
#include<stdio.h>
#define SWAP(a, b, c)(c t; t=a, a=b, b=t)
int main()
{
    int x=10, y=20;
    SWAP(x, y, int);
    printf("%d %d\n", x, y);
    return 0;
}
It compiles
Compiles with an warning
Not compile
Compiles and print nothing
Answer: Option
Explanation:
The code won't compile since declaration of t cannot occur within parenthesis.
Discussion:
25 comments Page 1 of 3.

Padhu said:   2 decades ago
Which parentheis that cannot occur

Jeho said:   1 decade ago
What ever we defined after # symbol is called preprocessor.

Here they used preprocessor function. Every function has to be declared before defined.

(a, b, c) is declaration in which t is not declared.

(c, t;t=a;a=b) is function t is used.

Wikiok said:   1 decade ago
After preprocessing:

int main()
{
int x=10, y=20;
(int t; t=x, x=y, y=t); //Error.
printf("%d %d\n", x, y);
return 0;
}

Madhusudan said:   1 decade ago
In the function call, at the place of c they have used the keyword int....that's also an error.

Kamal said:   1 decade ago
(c t; t=a, a=b, b=t), this statement is wrong. c can't be used for int and in expression "t=a, a=b, b=t" the symbol (,) should be replaced with (;).

Trilok chand soni said:   1 decade ago
The code won't compile because here in this program declaration of the cannot occur within parenthesis.

Samira said:   1 decade ago
In (c t;t=a,a=b,b=t) before execution we can not get the value of c hence t is not declared

Harikrishna said:   1 decade ago
#include<stdio.h>
#define SWAP(a, b, c){c t; t=a; a=b; b=t;}
int main()
{
int x=10, y=20;
SWAP(x, y, int);
printf("%d %d\n", x, y);
return 0;
}

The above altered program works successfully.....

Ravindra bagale said:   1 decade ago
@Samira.

Mam, here we can get the value of c as 'int' before.

c t, declaration. Only error is that, parenthesis must be replaced by {}and semicolons must be used at the comma.

i.e., {c t; t=a; a=b; b=t;}.

That's it.

Manish Atri said:   1 decade ago
@Ravindra Bagale You are right it works with
{c t; t=a; a=b; b=t;}


Post your comments here:

Your comments will be displayed after verification.