Exercise: C Preprocessor

Questions for: C Preprocessor

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: A

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

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: B

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.

What will be the output of the program?
#include<stdio.h>
#define FUN(arg) do\
                 {\
                    if(arg)\
                        printf("ExamAdept...", "\n");\
                  }while(--i)

int main()
{
    int i=2;
    FUN(i<3);
    return 0;
}
A:
ExamAdept...
ExamAdept...
ExamAdept
B:
ExamAdept... ExamAdept...
C:
Error: cannot use control instructions in macro
D:
No output
Answer: B

The macro FUN(arg) prints the statement "ExamAdept..." 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("ExamAdept...", "\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 "ExamAdept... ExamAdept..."

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: B

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[] = "ExamAdept";

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

    return 0;
}

Output:
-------
ExamAdept

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 ExamAdept 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.

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: B

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.

Ad Slot (Above Pagination)
Quiz