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?

4 Answers 4

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");
     // ...
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...");
}
7
  • 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
    Commented Oct 7, 2013 at 10:15
  • 1
    @zeal: You should use only the latter one. In case of Linux, both printf statements will be executed.
    – LihO
    Commented Oct 7, 2013 at 10:16
  • sorry the question would be if(condition) mistakely i had written if(true)
    – zeal
    Commented Oct 7, 2013 at 10:18
  • #ifndef _WINDOWS if(condition) printf("to do in linux"); else #endif {printf("should work in both linux and windows...");}
    – zeal
    Commented Oct 7, 2013 at 10:23
  • @zeal: See my edit. Should condition be evaluated in both Linux and Windows?
    – LihO
    Commented Oct 7, 2013 at 10:28
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.

2
  • Where have you been hiding these last 14 years or so? C99 introduced <stdbool.h> which defines bool, true and false. Commented Oct 7, 2013 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: 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 ;] Commented Oct 7, 2013 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

Not the answer you're looking for? Browse other questions tagged or ask your own question.