What should <условие> be for the code below to print “HelloWorld”?

 if (<условие>) printf("Hello"); else printf("World"); 
  • four
    I'm afraid you made a mistake of the universe ... - smackmychi
  • 3
    You can patch the code and zapat transitions :) Then the condition is not important. - KoVadim
  • Not using undefined behavior, no way. - VladD

4 answers 4

For example, <условие> could be !printf("Hello") .

  • 2
    Good answer. ) - Vladimir Gordeev
  • Then exit(printf("Hello World")) --- Well, that is, exit (printf ("hello world") * 0), 1 ideone.com/vHkyIX - VladD
  • Sense that? The author asked specifically about the condition. - smackmychi
  • @smackmychi: No, this is necessary instead of the condition. Look again at < ideone.com/vHkyIX >. - VladD 5:26
  • @VladD and here the author has a problem with the wording. Both branches, even in this case, will not be executed (question title). - smackmychi

Another option came to mind. He, of course, for aesthetes, but still. No one said that this should be an honest condition.

The condition must be as follows (symbol to symbol):

 1) printf("Hello"); if (0 

To make it invisible, we write this:

 #include <stdio.h> #define cond 1) printf("Hello"); if (0 int main(void) { if (cond) printf("Hello"); else printf("World"); return 0; } 

For those who do not understand how it works, make a substitution and format the code. It will be like this:

 #include <stdio.h> int main(void) { if (1) printf("Hello"); if (0) printf("Hello"); else printf("World"); return 0; } 
  • Yeah, then you can write it completely open if ((printf ("hello"), 0)) printf ("hello"); else printf ("world \ n"); - avp
  • Now it is a thread about the C-Obfuscation-contest. =) - Vladimir Gordeev
  • @avp: It was already in the top answer. - VladD
  • one
    For sure. Well, I didn’t notice the elephant (weighing 10) ... - avp

@ Bob1995 , I am not personally familiar with C, but if it is logical to think, then this will not work. Here is an example from life:

 if (в магазине есть мандарины) { идем в гости } else { идем домой } 

What should be in the store, that would have fulfilled both points?

  • The announcement that you were evicted from home? - VladD
  • @VladD, nobody kicked out anyone. ) - alvoro
  • 2
    @alvoro: Well, or if you are Winnie the Pooh and Piglet, then you go to visit and home at the same time (to Piglet). - VladD
  • @VladD, and these two here and ?! - alvoro

This will not work, you need another branch of the conditional operator, for example:

  if (<условие>) printf("Hello"); if (<условие>) printf("World");