0
   #ifndef _WINDOWS
   if(condition)
   {
     printf("to do in linux"); 
   }
   else
   #endif
   {
      printf("should work in both linux and windows...");
   }

My question: does this code work for both in linux and windows?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
zeal
  • 465
  • 2
  • 11
  • 22

4 Answers4

4

You have more logic than you need - you can just do this:

#ifndef _WINDOWS
     printf("to do in Linux"); 
     // ...
#endif
     printf("to do in both Linux and Windows");
     // ...
Paul R
  • 208,748
  • 37
  • 389
  • 560
2

Since #ifdef, #ifndef and #endif are preprocessor's directives, these will be applied before the compilation starts, ignoring the following lines completely in case _WINDOWS is defined:

#ifndef _WINDOWS
if(condition)
{
  printf("to do in linux"); 
}
else
#endif

Note that the condition will be evaluated only in case it is not on Windows.

On Windows, there will be nothing but the following nested anonymous scope:

{
   printf("should work in both linux and windows...");
}
LihO
  • 41,190
  • 11
  • 99
  • 167
  • thanks.. and in the case of linux it will be if(true) { printf("to do in linux") } else { printf("should work in both linux..."); } is this right? – zeal Oct 07 '13 at 10:15
  • 1
    @zeal: You should use only the latter one. In case of Linux, both `printf` statements will be executed. – LihO Oct 07 '13 at 10:16
  • sorry the question would be if(condition) mistakely i had written if(true) – zeal Oct 07 '13 at 10:18
  • #ifndef _WINDOWS if(condition) printf("to do in linux"); else #endif {printf("should work in both linux and windows...");} – zeal Oct 07 '13 at 10:23
  • @zeal: See my edit. Should `condition` be evaluated in both Linux and Windows? – LihO Oct 07 '13 at 10:28
  • @zeal: In that case your logic was correct all the time. See my last edit. – LihO Oct 07 '13 at 10:37
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38713/discussion-between-zeal-and-liho) – zeal Oct 07 '13 at 10:40
1

Actually, the ELSE statement won't run on Linux, because the compiled source code would be:

if(true)
{
   printf("to do in linux");
}
else
{
   printf("should work in both linux and windows and i have multiple statements in this else bolck");
}

And keep in mind that instead of true in C we have non-zero values, so if(1) for example (or you need a proper define for true keyword, or just `#include like @JonathanLeffler suggests).

But you could have definitely tried it with different defines in your code.

Piotr Zierhoffer
  • 5,005
  • 1
  • 38
  • 59
  • Where have you been hiding these last 14 years or so? C99 introduced `` which defines `bool`, `true` and `false`. – Jonathan Leffler Oct 07 '13 at 10:15
  • @JonathanLeffler Well, but what can we find there? `#define true 1 \n #define false 0`. `true` and `false` are NOT keywords, just defines. All is here: http://stackoverflow.com/a/4767943/882200 So my point is 100% valid and since the question was quite simple, the answer has to be precise. Or we could use jQuery for that and learn nothing ;] – Piotr Zierhoffer Oct 07 '13 at 10:59
0

It won't work on WINDOWS

Use this :

   #ifndef _WINDOWS

    printf("to do in linux"); 
    //...
    #else
    printf("should work in both linux and windows and i have multiple statements in this else bolck");
  //...Other blocks of code
 #endif
P0W
  • 46,614
  • 9
  • 72
  • 119