views:

621

answers:

13
+22  A: 

try fork() ! (and join afterwards). That's the only feature involving quantum mechanics that I know of.

fork() returns two distinct values at a time, which only the Schrödinger's cat is capable of.

NOTE: In a comment, Donal Fellows suggests:

You'll get more reliable behavior with !vfork()

Benoit
...and hope they print in the right order.
Wouldn't that be terrible programming practice? Why would they expect someone to know (let alone do) that in an interview? :/
townsean
I think they expect people to be imaginative…
Benoit
ah, I get why it works that way. Had to research fork :) the child pid is zero and the parent is some number. Interesting trick. If you were a C programmer familiar with multithreading, it might/should come easier to you.
townsean
@townsean: this is a great code to do fork, that way you make the father do one thing while the son is doing another. if the if was if (0 == fork()) it would do diffrent things for diffrent threads, and that is very desireable, for example if you want to execute an external process, and make the father wait till it finish.
none
good answer Benoit!
none
You'll get more reliable behavior with `!vfork()`
Donal Fellows
even better: `if (!vfork() || sleep(100))` should ensure correct order
No, `!vfork()` is enough; it's a tricky feature of vfork() that it suspends the parent until the child does execve() or exit().
Donal Fellows
Ah, so it does.
@none: ah, interesting. I can see it being very useful in that context. Thanks!
townsean
`vfork` is not reliable; in fact, `vfork` may simply be implemented as `fork`. Instead you need to use `fork` and `wait` to ensure synchronization.
R..
“Ensure synchronization” is for wimps.
Donal Fellows
A: 

No, it's not possible. Either the first block or the else block will get executed; that is the nature of an if statement.

Adrian Smith
A: 

There's no way to execute both branches of an if/else without exploiting it with weird things. If a condition is true then the else won't get executed and viceversa.

They're absolutely mutually exclusive.

Jack
A: 

Not without iterating over it again. I don't think you can do any Duff device like craziness in if statements. I suppose you could use goto, you say just by writing the condition.

andrewmu
A: 

In a debugger you could step through the if block, then move execution to the else block to execute, but that's just a silly answer to what seems to me to be a silly question.

Matthew Vines
+2  A: 

You can, but it's probably bad practice. Seems the interviewer was testing your knowledge of the language. Built with gcc 4.1.2.

#include <stdio.h>

int main() {
  if(!printf("hello ")) {
    printf("hello");
  } else {
    printf("world");
  }

  return 0;
}
Andrew Sledge
@Andrew: Please tell How....It will add to my knowledge ..:-)
Prashant
@Prashant: Updated with code
Andrew Sledge
+1  A: 

There is one possible way, it is not recommended.

if(check==true)
{
 printf("dont do this");
 goto condition1;
}
else
{
 condition1: ////this is crazy
 printf("Dont do this ever");
}
Kirit Chandran
+1 for warped thinking, but it fails the "without moving/adding any code" constraint?
Paul
You *cheated*! no way you can put anything inside the `{}` blocks according to the question :)
Benoit
You're right I did cheat, no soup for me. This question is pretty insane
Kirit Chandran
A: 

you could try :

if()
{
printf("hello");
goto Found;
}
else
{
Found: printf("world");
}
Catalin Florea
+9  A: 

If you can put in a macro definition as "part" of that condition...

if (1
#define else if (1)
)
{
    printf("hello");
}
else
{
    printf("world");
}

Then that code will indeed print "helloworld". It's a horrible, dirty trick though; my soul is probably in trouble for mentioning it!

Donal Fellows
Yeah, that's great too :) Don't forget to `#undef` it. I think you could just `#define else`. It will just scope the `printf("world");`.
Benoit
I don't get the idea...
Ichibann
this will do : `if(1) {printf("hello");} if(1) {printf("world");}`
Benoit
thanks Donal.....It's Ok....But it doesn't make any sense...Though it achieves the intended aim very well. lets say " by Hook or by CROOK!!"
Prashant
@Prashant: Sense?! You're getting deep voodoo hackery tricks here, not good structured programming. :-)
Donal Fellows
A: 

The easiest way I think is this:

//if(.......)
{
    printf("hello");
}
//else
{
    printf("world");
}
Sergio
`else` can't exists without `if`.
The Elite Gentleman
My bad :) Fixed it
Sergio
This question states `.You have to write the missing condition in if statement.. `
The Elite Gentleman
+2  A: 

It depends on the exact phrasing of the question. As you describe it, the question does not ask you to execute both branches of the if statement. It asks you to insert a condition that results in printing "hello world" without changing anything else.

If you have complete freedom in what you put in the condition, you can use this solution:

 if(printf("hello ") == -1)
 {
     printf("hello");
 }
 else
 {
     printf("world");
 }

However, the solution uses "printf" in the condition, which is ruled out by one of the rules you gave. What's not clear to me is whether the printf prohibition also applies to what you write in the condition.

Note: Answer is edited to reflect the comments. The original answer ignored the prohibition on print statements.

Larry Lustig
N 1.1
The problem is… the question asks for no additional printing statements. While you could try arguing before the judge that the `printf` you inserted is a printing expression (tricky!) I suspect it's not going to get you credit. Nice try though.
Donal Fellows
Well, you guys may well be right depending on the original phraseology of the question. If you're allowed to enter anything you want in the condition (and the prohibition on adding code only applies outside the condition) then the solution stands. But if the prohibition applies inside the condition as well, obviously my solution is invalid.
Larry Lustig
A: 

Wouldn't this work?

if !(printf("hello")){
}
else{
   printf(" world");
}
singhularity
The Elite Gentleman
...so much for my first answer here :)
singhularity
+13  A: 

This will work

if(schrodingers_cat_is_dead())
{
    printf("hello");
}
else
{
    printf("world");
}

Unfortunately, as soon as you look at the output, not only will it collapse into the state of only producing "hello" or "world", but there's a 50% chance you will have murdered a cat.

JeremyP
This was pretty funny. Thanks.
San Jacinto