16.
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=”Hello”;
char *str2=”Hi”;
JOIN(str1, str2);
return 0;
}
A. str1=HelloHi str2=Hi
B. str1=Hello str2=Hi
C. str1=Hello str2=HelloHi
D. Error: in macro substitution
17.
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. 60, 6
Answer: Option C (Turbo C)Option D (CodeBlocks)
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.
18.
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
Answer: Option A
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.
19.
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’
Answer: Option B
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.
20.
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
Answer: Option B
Explanation:
The following program will make you understand about ## (macro concatenation) operator clearly.
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.