Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to 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 20-Nov-12 4:18am
Edited 20-Nov-12 4:33am
v2
Comments
joshrduncan2012 at 20-Nov-12 9:20am
   
Have you tried running the code in a compiler to see what output you receive?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

It is (a).
because:
  • My compiler told me. Smile | :)
  • 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.
  Permalink  
Comments
nv3 at 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 at 20-Nov-12 11:18am
   
Yes.
Thank you.
alman hossain at 21-Nov-12 2:15am
   
Syntax of conditional preprocessor directive (if) is:
#if
#else
#endif
In this code (a==0) is not constant expression.so answer is (E).I am still confused!!
CPallini at 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 R. Bihy at 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! :)
CPallini at 20-Nov-12 16:37pm
   
There is, e.g. the famous
#ifdef __cpluplus
However sometimes you need to evaluate expressions, like in, for instance
#if (WINVER >= 0x0600)
Manfred R. Bihy at 20-Nov-12 16:44pm
   
I do understand, but it's still silly to assume somthing that is not defined is equal to zero when used in an expression.
Me no like!
I get pretty anal about stuff like that. :)
Sergey Alexandrovich Kryukov at 20-Nov-12 19:28pm
   
It's not just something you rightfully dislike, this is actually wrong assumption. Too bad compilers still allow it.
--SA
Sergey Alexandrovich Kryukov at 20-Nov-12 19:26pm
   
Amazing mix of pre-compiler and compiler code. I don't know of IP got it or not, but that's a 5.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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:
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.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 20-Nov-12 19:27pm
   
Sure, a 5.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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!
  Permalink  
v4
Comments
CPallini at 20-Nov-12 9:40am
   
You didn't try to compile it, did you?
Manfred R. Bihy at 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 at 20-Nov-12 13:31pm
   
To be honest, before trying it with the compiler, I made your same hypothesis.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

It would be (A) as #if statement will always thread a==0 as true
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 345
1 OriginalGriff 210
2 DamithSL 210
3 Kornfeld Eliyahu Peter 210
4 Peter Leow 95
0 OriginalGriff 7,215
1 DamithSL 5,199
2 Maciej Los 4,866
3 Sergey Alexandrovich Kryukov 4,757
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100