C Programming - C Preprocessor - Discussion

2. 

Point out the error in the program

#include<stdio.h>

int main()
{
    int i;
    #if A
        printf("Enter any number:");
        scanf("%d", &i);
    #elif B
        printf("The number is odd");
    return 0;
}

[A]. Error: unexpected end of file because there is no matching #endif
[B]. The number is odd
[C]. Garbage values
[D]. None of above

Answer: Option A

Explanation:

The conditional macro #if must have an #endif. In this program there is no #endif statement written.


Meena said: (Sep 17, 2011)  
Can we include the prreprocessors inside the program?

Preeti said: (Dec 19, 2012)  
Yes I think. Because the above program gives output without errors when elif is replaced by endif.

Huma said: (Mar 16, 2013)  
Yes we can define macros within main().

Jagadesh said: (Oct 12, 2014)  
int main ( )
{
int i;
for(;scanf("%d",&i);printf("%d",i));

return 0;
}

Can any one give me the output?

Shekhar said: (Dec 16, 2014)  
@Jagadesh.

Infinite loop as the for loop will execute infinite number of scanf statements and therefore printf also. If still doubt, do comment. :).

Vishalakshi said: (Dec 7, 2016)  
Hii friends.

You are right @Shekhar. It gives an infinite loop.

Rohan said: (Jun 10, 2018)  
Here it is;

int i;
Step 1: a variable(i) is declared.

#if A
printf("Enter any number:");
scanf("%d", &i);
Step 2: (i) Macro() or preprocessor can be used inside main() function..
(ii) It will execute the if condition

#elif B
printf("The number is odd");

Step 3: It will execute elif part too but #endif is missing from this program and so it will give an error.

Shubh said: (Mar 21, 2020)  
What is the A and B means?

Ritik said: (Aug 25, 2020)  
A and B are macros @Shubh.

Amol Pansare said: (Jul 8, 2021)  
@Rohan.

Very good explanation, Thanks.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.