C Programming - C Preprocessor - Discussion

Discussion Forum : C Preprocessor - Find Output of Program (Q.No. 9)
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;
}
IndiaBIX...
IndiaBIX...
IndiaBIX
IndiaBIX... IndiaBIX...
Error: cannot use control instructions in macro
No output
Answer: Option
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..."

Discussion:
22 comments Page 2 of 3.

Ishaan said:   10 years ago
I agree with @Vince it seems an infinite loop.

Nejat sultan said:   10 years ago
2<3....2.

1<3....1.

This time while (0). It fails here. So functions 2 times.

Nagalakshmi said:   10 years ago
Yea it is correct, here while(0) becomes an infinite loop. That's why the condition became fail at i=0.

Purnima said:   8 years ago
In while(0) we will just check the value of I which is zero so loops terminate. We are not checking i<3.

Hosam said:   8 years ago
How could they use (i) at preprocessing time before it declared in main at compile time?

Abhijit said:   8 years ago
do
{
if(2 < 3)
printf("IndiaBIX...", "\n");
}while(--2)

There is a "\n". it should have taken printf to the next line. I am a little bit confuse, please someone explain it.
(3)

Prashant said:   8 years ago
Yes, I agree @Abhijit.

Kalyan babu said:   7 years ago
Yes, I agree @Abhijit, @Prashant.

Shrividya said:   7 years ago
Even 0 is less than 3 than why does while loop break?

Shrividya said:   7 years ago
I got it 0 indicates false in programming language.


Post your comments here:

Your comments will be displayed after verification.