Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include<stdio.h>
int main(){
    int a=0;
    #if (a==0)
         printf("Equal");
    #else if
         printf("Not equal");
    #endif
    return 0;
}
what will be the output..
A) Equal
(B) Not equal
(C) Null
(D) Garbage
(E) Compilation error

is it (a) or (e)

please any body explain
Posted
Updated 20-Nov-12 3:33am
v2
Comments
joshrduncan2012 20-Nov-12 9:20am    
Have you tried running the code in a compiler to see what output you receive?

It is (a).
because:
  • My compiler told me. :-)
  • Documentation[^] states: "The `#if' directive allows you to test the value of an arithmetic expression [...] expression is a C expression of integer type, subject to stringent restrictions. It may contain [...] Identifiers that are not macros, which are all considered to be the number zero.
 
Share this answer
 
Comments
nv3 20-Nov-12 9:59am    
You are so right! Just to clarify, #if (a == 0) does not turn out as true, because of the previous declaration of "int a = 0;". It looks for preprocessor symbol called a, doesn't find one, and accordingly assumes a default value of 0. To check, delete the "int a = 0;" line and the result will stay the same. +5
CPallini 20-Nov-12 11:18am    
Yes.
Thank you.
alman hossain 21-Nov-12 2:15am    
Syntax of conditional preprocessor directive (if) is:
#if <constant expression="">
#else
#endif
In this code (a==0) is not constant expression.so answer is (E).I am still confused!!
CPallini 21-Nov-12 2:55am    
The key is (from the point of view of the preprocessor): "Identifiers that are not macros, which are all considered to be the number zero". The identifier a is not a macro hence it is 0.
Manfred Rudolf Bihy 20-Nov-12 16:21pm    
Well, one never stops learning! Thanks for checking that out
I hate stuff like that though: Oh let's see it's undefined so I'll just go on assuming it's zero. Stuff like that freaks me out.
On a similar note: Isn't there some preprocessor directive like ifdev or ifdefined to test if a symbol is defined or not. My last C programming is quite a while back, but I think there was something like that, somewhere at least. Not sure which one though as I also did some embedded programmng (Keil), some Unix stuff (mostly GNU) and then there were some rare occasions with MS VC. A long, long, long time ago! :)
You should not be using the #if preprocessor macros as these affect the generation of the code, and not the executable program. Your code should be:
C++
int main(){
    int a=0;
    if (a==0)
         printf("Equal");
    else
         printf("Not equal");
    return 0;
}

Try building this and executing with different values of a.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Nov-12 19:27pm    
Sure, a 5.
--SA
The answer should be E.
a is here a variable declared in the code and not a symbol used in conditional compilation as indicated by the #if token.


The answer is of course A. Thanks and credit goes to CPallini[^] for his well founded Solution 2[^]. (I tend to make assumptions how certain software products should work simply based on my own diligence. This, however, is an error as we easily observed in this sample.)


Cheers!
 
Share this answer
 
v4
Comments
CPallini 20-Nov-12 9:40am    
You didn't try to compile it, did you?
Manfred Rudolf Bihy 20-Nov-12 12:54pm    
Nope, just a wild guess. I was hoping for the best in those C compilers. Looks as if I was wrong then?

BTW, it's hard trying code and compiling on my Samsung Galaxy Tablet.

:)
Cheers!
CPallini 20-Nov-12 13:31pm    
To be honest, before trying it with the compiler, I made your same hypothesis.
It would be (A) as #if statement will always thread a==0 as true
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900