21.
What will be the output of the program?
#include<stdio.h>
#define FUN(i, j) i##j
int main()
{
int First = 10;
int Second = 20;
char FirstSecond[] = “Catalyst”;
printf(“%s\n”, FUN(First, Second) );
return 0;
}
A. 10
B. 20
C. 1020
D. Catalyst
Answer: Option D
Explanation:
Output:
——-
Catalyst
The following program will make you understand about ## (macro concatenation) operator clearly.
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 Catalyst as output.
22.
What will be the output of the program?
#include<stdio.h>
#define FUN(arg) do\
{\
if(arg)\
printf(“Catalyst…”, “\n”);\
}while(–i)
int main()
{
int i=2;
FUN(i<3);
return 0;
}
A. Catalyst…
Catalyst…
Catalyst
B. Catalyst… Catalyst…
C. Error: cannot use control instructions in macro
D. No output
Answer: Option B
Explanation:
The macro FUN(arg) prints the statement “Catalyst…” 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(“Catalyst…”, “\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 “Catalyst… Catalyst…”
23.
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
Answer: Option B
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.
24.
What will be the output of the program?
#include<stdio.h>
#define MIN(x, y) (x<y)? x : y;
int main()
{
int x=3, y=4, z;
z = MIN(x+y/2, y-1);
if(z > 0)
printf(“%d\n”, z);
return 0;
}
A. 3
B. 4
C. 0
D. No output
Answer: Option A
Explanation:
The macro MIN(x, y) (x<y)? x : y; returns the smallest value from the given two numbers.
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
25.
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
Answer: Option C
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”