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;
}
Answer: Option
Explanation:
The code won't compile since declaration of t cannot occur within parenthesis.
Discussion:
25 comments Page 3 of 3.
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 (;).
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.
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;
}
int main()
{
int x=10, y=20;
(int t; t=x, x=y, y=t); //Error.
printf("%d %d\n", x, y);
return 0;
}
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.
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.
Padhu said:
2 decades ago
Which parentheis that cannot occur
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers