C Programming - C Preprocessor

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

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.


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'

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.


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

Answer: Option B

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

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

    return 0;
}

Output:
-------
IndiaBIX

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


9. 

What will be the output of the program?

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

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

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

Answer: Option B

Explanation:

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


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

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.