Click here to Skip to main content
Sign Up to vote bad
good
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 - 3:18
Edited 20 Nov '12 - 3:33

Comments
joshrduncan2012 - 20 Nov '12 - 9:20
Have you tried running the code in a compiler to see what output you receive?

4 solutions

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 - 20 Nov '12 - 9:59
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:18
Yes. Thank you.
alman hossain - 21 Nov '12 - 2:15
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 - 21 Nov '12 - 2:55
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 - 20 Nov '12 - 16:21
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 - 20 Nov '12 - 16:37
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 - 20 Nov '12 - 16:44
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 - 20 Nov '12 - 19:28
It's not just something you rightfully dislike, this is actually wrong assumption. Too bad compilers still allow it. --SA
Sergey Alexandrovich Kryukov - 20 Nov '12 - 19:26
Amazing mix of pre-compiler and compiler code. I don't know of IP got it or not, but that's a 5. --SA
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 - 20 Nov '12 - 19:27
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!
  Permalink  
Comments
CPallini - 20 Nov '12 - 9:40
You didn't try to compile it, did you?
Manfred R. Bihy - 20 Nov '12 - 12:54
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:31
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
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 415
1 OriginalGriff 325
2 Arun Vasu 268
3 CPallini 213
4 Tadit Dash 203
0 Sergey Alexandrovich Kryukov 10,005
1 OriginalGriff 7,654
2 CPallini 4,171
3 Rohan Leuva 3,447
4 Maciej Los 2,974


Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid