### Find Output Of Program

Question 1 :

What will be the output of the program?

``````#include < stdio.h >
#define MAN(x, y) ((x)>(y)) ? (x):(y);

int main()
{
int i=10, j=5, k=0;
k = MAN(++i, j++);
printf("%d, %d, %d\n", i, j, k);
return 0;
}``````

A). 12, 6, 12
B). 11, 5, 11
C). 11, 5, Garbage
D). 12, 6, Garbage

Explanation :

The macro MAN(x, y) ((x)>(y)) ? (x):(y); returns the biggest number of given two numbers.
Step 1: int i=10, j=5, k=0; The variable i, j, k are declared as an integer type and initialized to value 10, 5, 0 respectively.

Step 2: k = MAN(++i, j++); becomes,
=> k = ((++i)>(j++)) ? (++i):(j++);
=> k = ((11)>(5)) ? (12):(6);
=> k = 12

Step 3: printf("%d, %d, %d\n", i, j, k); It prints the variable i, j, k.

In the above macro step 2 the variable i value is increemented by 2 and variable j value is increemented by 1.
Hence the output of the program is 12, 6, 12

Question 2 :

What will be the output of the program?

``````#include < stdio.h >
#define SQUARE(x) x*x
int main()
{
float s=10, u=30, t=2, a;
a = 2*(s-u*t)/SQUARE(t);
printf("Result = %f", a);
return 0;
}``````

A). Result = -100.000000
B). Result = -25.000000
C). Result = 0.000000
D). Result = 100.000000

Explanation :

The macro function SQUARE(x) x*x calculate the square of the given number 'x'. (Eg: 102)

Step 1: float s=10, u=30, t=2, a; Here the variable s, u, t, a are declared as an floating point type and the variable s, u, t are initialized to 10, 30, 2.

Step 2: a = 2*(s-u*t)/SQUARE(t); becomes,
=> a = 2 * (10 - 30 * 2) / t * t; Here SQUARE(t) is replaced by macro to t*t .
=> a = 2 * (10 - 30 * 2) / 2 * 2;
=> a = 2 * (10 - 60) / 2 * 2;
=> a = 2 * (-50) / 2 * 2 ;
=> a = 2 * (-25) * 2 ;
=> a = (-50) * 2 ;
=> a = -100;

Step 3: printf("Result=%f", a); It prints the value of variable 'a'.

Hence the output of the program is -100

Question 3 :

What will be the output of the program?

``````#include < stdio.h >
#define SQR(x)(x*x)

int main()
{
int a, b=3;
a = SQR(b+2);
printf("%d\n", a);
return 0;
}``````

A). 25
B). 11
C). Error
D). Garbage value

Explanation :

The macro function SQR(x)(x*x) calculate the square of the given number 'x'. (Eg: 102)

Step 1: int a, b=3; Here the variable a, b are declared as an integer type and the variable b is initialized to 3.

Step 2: a = SQR(b+2); becomes,
=> a = b+2 * b+2; Here SQR(x) is replaced by macro to x*x .
=> a = 3+2 * 3+2;
=> a = 3 + 6 + 2;
=> a = 11;

Step 3: printf("%d\n", a); It prints the value of variable 'a'.

Hence the output of the program is 11

Question 4 :

What will be the output of the program?

``````#include < stdio.h >
#define JOIN(s1, s2) printf("%s=%s %s=%s \n", #s1, s1, #s2, s2);
int main()
{
char *str1="India";
char *str2="Parinam";
JOIN(str1, str2);
return 0;
}``````

A). str1=IndiaParinam str2=Parinam
B). str1=India str2=Parinam
C). str1=India str2=IndiaParinam
D). Error: in macro substitution

Question 5 :

What will be the output of the program?

``````#include < stdio.h >
#define CUBE(x) (x*x*x)

int main()
{
int a, b=3;
a = CUBE(b++);
printf("%d, %d\n", a, b);
return 0;
}``````

A). 9, 4
B). 27, 4
C). 27, 6
D). Error

Explanation :

The macro function CUBE(x) (x*x*x) calculates the cubic value of given number(Eg: 103.)

Step 1: int a, b=3; The variable a and b are declared as an integer type and varaible b id initialized to 3.

Step 2: a = CUBE(b++); becomes
=> a = b++ * b++ * b++;
=> a = 3 * 3 * 3; Here we are using post-increement operator, so the 3 is not incremented in this statement.
=> a = 27; Here, 27 is store in the variable a. By the way, the value of variable b is incremented by 3. (ie: b=6)

Step 3: printf("%d, %d\n", a, b); It prints the value of variable a and b.

Hence the output of the program is 27, 6.

Question 6 :

What will be the output of the program?

``````#include < stdio.h >
#define PRINT(int) printf("int=%d, ", int);
int main()
{
int x=2, y=3, z=4;
PRINT(x);
PRINT(y);
PRINT(z);
return 0;
}``````

A). int=2, int=3, int=4
B). int=2, int=2, int=2
C). int=3, int=3, int=3
D). int=4, int=4, int=4

Explanation :

The macro PRINT(int) print("%d,", int); prints the given variable value in an integer format.

Step 1: int x=2, y=3, z=4; The variable x, y, z are declared as an integer type and initialized to 2, 3, 4 respectively.

Step 2: PRINT(x); becomes printf("int=%d,",x). Hence it prints 'int=2'.

Step 3: PRINT(y); becomes printf("int=%d,",y). Hence it prints 'int=3'.

Step 4: PRINT(z); becomes printf("int=%d,",z). Hence it prints 'int=4'.

Hence the output of the program is int=2, int=3, int=4.

Question 7 :

What will be the output of the program?

``````#include < stdio.h >
#define SWAP(a, b) int t; t=a, a=b, b=t;
int main()
{
int a=10, b=12;
SWAP(a, b);
printf("a = %d, b = %d\n", a, b);
return 0;
}``````

A). a = 10, b = 12
B). a = 12, b = 10
C). Error: Declaration not allowed in macro
D). Error: Undefined symbol 't'

Explanation :

The macro SWAP(a, b) int t; t=a, a=b, b=t; swaps the value of the given two variable.

Step 1: int a=10, b=12; The variable a and b are declared as an integer type and initialized to 10, 12 respectively.

Step 2: SWAP(a, b);. Here the macro is substituted and it swaps the value to variable a and b.
Hence the output of the program is 12, 10.

Question 8 :

What will be the output of the program?

``````#include < stdio.h >
#define FUN(i, j) i##j

int main()
{
int va1=10;
int va12=20;
printf("%d\n", FUN(va1, 2));
return 0;
}``````

A). 10
B). 20
C). 1020
D). 12

Explanation :

The following program will make you understand about ## (macro concatenation) operator clearly.

``````#include < stdio.h >
#define FUN(i, j) i##j

int main()
{
int First  	= 10;
int Second  = 20;

char FirstSecond[] = "IndiaParinam";

printf("%s\n", FUN(First, Second) );

return 0;
}``````
Output:

-------
IndiaParinam

The preprocessor will replace FUN(First, Second) as FirstSecond.
Therefore, the printf("%s\n", FUN(First, Second) ); statement will become as printf("%s\n", FirstSecond );

Hence it prints IndiaParinam as output.
Like the same, the line printf("%d\n", FUN(va1, 2)); given in the above question will become as printf("%d\n", va12 );.
Therefore, it prints 20 as output.

Question 9 :

What will be the output of the program?

``````#include < stdio.h >
#define FUN(arg) do\
{\
if(arg)\
printf("IndiaParinam...", "\n");\
}while(--i)

int main()
{
int i=2;
FUN(i<3);
return 0;
}``````

A). IndiaParinam... IndiaParinam... IndiaParinam
B). IndiaParinam... IndiaParinam...
C). Error: cannot use control instructions in macro
D). No output

Explanation :

The macro FUN(arg) prints the statement "IndiaParinam..." untill the while condition is satisfied.

Step 1: int i=2; The variable i is declared as an integer type and initialized to 2.

Step 2: FUN( i < 3 ); becomes,

``````do
{
if(2 < 3)
printf("IndiaParinam...", "\n");
}while(--2)``````
After the 2 while loops the value of i becomes '0'(zero). Hence the while loop breaks.
Hence the output of the program is "IndiaParinam... IndiaParinam..."

Question 10 :

What will be the output of the program?

``````#include < stdio.h >
#define MAX(a, b) (a > b ? a : b)
int main()
{
int x;
x = MAX(3+2, 2+7);
printf("%d\n", x);
return 0;
}``````

A). 8
B). 9
C). 6
D). 5

Explanation :

The macro MAX(a, b) (a > b ? a : b) returns the biggest value of the given two numbers.

Step 1 : int x; The variable x is declared as an integer type.

Step 2 : x = MAX(3+2, 2+7); becomes,

=> x = (3+2 > 2+7 ? 3+2 : 2+7)
=> x = (5 > 9 ? 5 : 9)
=> x = 9

Step 3 : printf("%d\n", x); It prints the value of variable x.

Hence the output of the program is 9.

Question 11 :

What will be the output of the program?

``````#include < stdio.h >
#define MIN (x, y) (x 0)
printf("%d\n", z);
return 0;
}``````

A). 3
B). 4
C). 0
D). No output

Explanation :

The macro MIN(x, y) (x
Step 1: int x=3, y=4, z; The variable x, y, z are declared as an integer type and the variable x, y are initialized to value 3, 4 respectively.

Step 2: z = MIN(x+y/2, y-1); becomes,
=> z = (x+y/2 < y-1)? x+y/2 : y - 1;
=> z = (3+4/2 < 4-1)? 3+4/2 : 4 - 1;
=> z = (3+2 < 4-1)? 3+2 : 4 - 1;
=> z = (5 < 3)? 5 : 3;
The macro return the number 3 and it is stored in the variable z.

Step 3: if(z > 0) becomes if(3 > 0) here the if condition is satisfied. It executes the if block statements.

Step 4: printf("%d\n", z);. It prints the value of variable z.

Hence the output of the program is 3

Question 12 :

What will be the output of the program?

``````#include < stdio.h >
#define str(x) #x
#define Xstr(x) str(x)
#define oper multiply

int main()
{
char *opername = Xstr(oper);
printf("%s\n", opername);
return 0;
}``````

A). Error: in macro substitution
B). Error: invalid reference 'x' in macro
C). print 'multiply'
D). No output

Explanation :

The macro #define str(x) #x replaces the symbol 'str(x)' with 'x'.
The macro #define Xstr(x) str(x) replaces the symbol 'Xstr(x)' with 'str(x)'.
The macro #define oper multiply replaces the symbol 'oper' with 'multiply'.

Step 1: char *opername = Xstr(oper); The varible *opername is declared as an pointer to a character type.
=> Xstr(oper); becomes,
=> Xstr(multiply);
=> str(multiply)
=> char *opername = multiply

Step 2: printf("%s\n", opername); It prints the value of variable opername.

Hence the output of the program is "multiply"

Question 13 :

What will be the output of the program?

``````#include < stdio.h >
#define MESS junk

int main()
{
printf("MESS\n");
return 0;
}``````

A). junk
B). MESS
C). Error
D). Nothing will print

Explanation :

printf("MESS\n"); It prints the text "MESS". There is no macro calling inside the printf statement occured.

Question 14 :

What will be the output of the program?

``````#include < stdio.h >
#define PRINT(i) printf("%d,",i)

int main()
{
int x=2, y=3, z=4;
PRINT(x);
PRINT(y);
PRINT(z);
return 0;
}``````

A). 2, 3, 4,
B). 2, 2, 2,
C). 3, 3, 3,
D). 4, 4, 4,

Explanation :

The macro PRINT(i) print("%d,", i); prints the given variable value in an integer format.

Step 1: int x=2, y=3, z=4; The variable x, y, z are declared as an integer type and initialized to 2, 3, 4 respectively.
Step 2: PRINT(x); becomes printf("%d,",x). Hence it prints '2'.
Step 3: PRINT(y); becomes printf("%d,",y). Hence it prints '3'.
Step 4: PRINT(z); becomes printf("%d,",z). Hence it prints '4'.

Hence the output of the program is 2, 3, 4.

Question 15 :

What will be the output of the program?

``````#include < stdio.h >
#define MAX(a, b, c) (a>b ? a>c ? a : c: b>c ? b : c)

int main()
{
int x;
x = MAX(3+2, 2+7, 3+7);
printf("%d\n", x);
return 0;
}``````

A). 5
B). 9
C). 10
D). 3+7

Explanation :

The macro MAX(a, b, c) (a>b ? a>c ? a : c: b>c ? b : c) returns the biggest of given three numbers.

Step 1: int x; The variable x is declared as an integer type.
Step 2: x = MAX(3+2, 2+7, 3+7); becomes,
=> x = (3+2 >2+7 ? 3+2 > 3+7 ? 3+2 : 3+7: 2+7 > 3+7 ? 2+7 : 3+7)
=> x = (5 >9 ? (5 > 10 ? 5 : 10): (9 > 10 ? 9 : 10) )
=> x = (5 >9 ? (10): (10) )
=> x = 10
Step 3: printf("%d\n", x); It prints the value of 'x'.

Hence the output of the program is "10".