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.

Rahul Sharma said:   6 years ago
@Ganga.

You can use small letters.

When you are define the small letters in above programme.

Ganga said:   7 years ago
Why the swap is in header file. We can use the Capital letters when writing the c programme? Please tell me.

Anup singh said:   7 years ago
Hey, friends. Can anyone explain me the header file? Please.

#include

And why it is written in this bracket < >?

Kumaran said:   8 years ago
(c t; t=a, a=b, b=t) please explain this.

Vishwadeep dubey said:   8 years 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.
(1)

Surya said:   8 years ago
main()
{
while(printf(""))
{
printf("gopi");
}
}

Please tell me execution process.

Urvashi Dhakad said:   9 years 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;
}

MonikaS said:   10 years 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;
}

Gupta said:   10 years 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.

Puneet said:   10 years ago
How to correct this program then? (passing 'int' as arg).


Post your comments here:

Your comments will be displayed after verification.