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 1 of 3.
Urvashi Dhakad said:
1 decade ago
@Sumit bhau.
The function add should be declared first before it has been called in the main function and also there will be a return type of the function add, like this:
#include<stdio.h>
#include<conio.h>
int add(int, int,int);
int main()
{
int a,b,c,sum ;
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();
}
int add (int a,int b,int c )
{
int d;
d=a+b+c ;
return d;
}
The function add should be declared first before it has been called in the main function and also there will be a return type of the function add, like this:
#include<stdio.h>
#include<conio.h>
int add(int, int,int);
int main()
{
int a,b,c,sum ;
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();
}
int add (int a,int b,int c )
{
int d;
d=a+b+c ;
return d;
}
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;
}
MonikaS said:
1 decade ago
@Sumit Bhau.
The function add should return d,
i.e,
#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 ;
return d;
}
The function add should return d,
i.e,
#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 ;
return d;
}
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;
}
Gupta said:
1 decade ago
#include<stdio.h>
#include<conio.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);
getch();
return 0;
}
See this will work and will give x=20 y =10.
#include<conio.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);
getch();
return 0;
}
See this will work and will give x=20 y =10.
(1)
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.
Vishwadeep dubey said:
1 decade ago
#include<stdio.h>
int c,t;
#define SWAP(a,b,c) (t=a,a=b,b=c)
int main()
{
int x=10,y=20;
SWAP(x,y,t);
printf("x=%d,y=%d\n",x,y);
return 0;
}
Above program works correctly and swap the value of x and y.
int c,t;
#define SWAP(a,b,c) (t=a,a=b,b=c)
int main()
{
int x=10,y=20;
SWAP(x,y,t);
printf("x=%d,y=%d\n",x,y);
return 0;
}
Above program works correctly and swap the value of x and y.
(5)
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.....
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers