C Programming - C Preprocessor - Discussion

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.


Yashwant Kanetkar said: (Aug 12, 2012)  
When pre-processor replaced the macro with its corresponding expansion, it only substituted the int outside double quotes, and one within quotes untouched.

Yogeshwar Singh said: (Feb 11, 2015)  
This is because the int inside the double quotes doesn't have space between 'int', '=' and '%d', so pre-processor treats 'int=%d' as a single string and that is why the int inside the quotes didn't get substituted.

Chetan said: (Apr 14, 2015)  
But as per my knowledge print returns the size of string it is printing so why the print not printing the size of strings which print is printing.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.