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 2 of 3.
Sumit bhau said:
1 decade ago
What is the error in this program and where?
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c,sum ;
clrscr();
printf("Enter the values of a , b , c :");
scanf("%d%d%d",&a,&b,&c);
sum = add (a,b,c);
printf("\n sum = %d",sum);
getch();
}
add (int a,int b,int c )
//int x,y,z;
{
int d;
d=a+b+c ;
getch();
return 0;
}
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c,sum ;
clrscr();
printf("Enter the values of a , b , c :");
scanf("%d%d%d",&a,&b,&c);
sum = add (a,b,c);
printf("\n sum = %d",sum);
getch();
}
add (int a,int b,int c )
//int x,y,z;
{
int d;
d=a+b+c ;
getch();
return 0;
}
Bhaskar said:
1 decade ago
It will work with {} or directly,
#define SWAP(a, b, c) c t; t=a, a=b, b=t.
As after compiling it become something like this which is a error.
#include<stdio.h>
int main()
{
int x=10, y=20;
(int t;t=x,x=y,y=t);
printf("%d %d\n", x, y);
return 0;
}
#define SWAP(a, b, c) c t; t=a, a=b, b=t.
As after compiling it become something like this which is a error.
#include<stdio.h>
int main()
{
int x=10, y=20;
(int t;t=x,x=y,y=t);
printf("%d %d\n", x, y);
return 0;
}
Vinay said:
1 decade ago
Why it is not working with parenthesis, while at the same time working with {}?
Sandeep said:
1 decade ago
#define SWAP(a, b, c){c t; t=a,a=b,b=t;} // this statement is wrong
SWAP(x, y, int); //here 3rd variable is not declare
SWAP(x, y, int); //here 3rd variable is not declare
Amer 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;
}
This code will also work fine.
#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;
}
This code will also work fine.
Manish Atri said:
1 decade ago
@Ravindra Bagale You are right it works with
{c t; t=a; a=b; b=t;}
{c t; t=a; a=b; b=t;}
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.
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.
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.....
#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.....
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
Trilok chand soni said:
1 decade ago
The code won't compile because here in this program declaration of the cannot occur within parenthesis.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers