PDA

View Full Version : Make main() return 3 - PUZZLE



Queatrix
12-31-2005, 02:20 PM
#include <iostream>
int i;
int main( void )
{
i = 3;
for(;;)
{
/* INSERT LINE HERE TO MAKE MAIN() RETURN 3 - LINE MUST BEGIN WITH i */
i = 2;
break;
}
return 0;
}

adrianxw
12-31-2005, 02:28 PM
if (i != 0) return 3;

kermit
12-31-2005, 02:30 PM
;) good one - I was thinking of 'i' as in the variable, 'i' and not as in 'i'f

It might be fun to stipulate that you cannot use the return keyword in the line of code..

::evil laugh::

cboard_member
12-31-2005, 03:06 PM
Damn I've got to think how to do that in assembly and start the line with i. Maybe // i before the asm block with suffice. Yeah, that'll do.



//i
__asm {
cmp i, 0
je contin
mov eax, 3
ret
contin:
}


Maybe? Feel free to slap the assembly amateur :)
EDIT: Cool-August, why the hell is i global?

anonytmouse
12-31-2005, 03:31 PM
i; return 3;

EDIT:Does kermit == Cool-August?

bithub
12-31-2005, 03:42 PM
i ? (exit(3)) : 0;

kermit
12-31-2005, 04:06 PM
i; return 3;

EDIT:Does kermit == Cool-August?

Nope, kermit == kermit.

Anyway, here is my contribution:



i = 0; asm("movq $1, %rax\n\tmovq $3, %rbx\n\tint $0x80");


I know, its cheap... :)

Queatrix
12-31-2005, 04:17 PM
Allright, to make it harder, the new line must also have no parenthasis and no spaces.
It's still possible.

>> EDIT: Cool-August, why the hell is i global?

Beacuse now if it wasn't it wouldn't be possible.

EDIT: Also, now you can't use more than one command.

Sang-drax
12-31-2005, 06:39 PM
Allright, to make it harder, the new line must also have no parenthasis and no spaces.
[...]
EDIT: Also, now you can't use more than one command.
These lines work:


i:return+i;
i:return+3;

:)

Rashakil Fol
12-31-2005, 06:41 PM
i;return!0+3;

[edit]Doh!

Sang-drax
12-31-2005, 06:44 PM
i;return!0+3;

Your code returns 4
Your code contains more than one statement. "More than one command" is disallowed.

Rashakil Fol
12-31-2005, 06:44 PM
Beacuse now if it wasn't it wouldn't be possible.

Why not? Maybe I'm missing something...

Sang-drax
12-31-2005, 06:48 PM
Why not? Maybe I'm missing something...
I agree, that sounds strange. What special property do global variables have?

Dave_Sinkula
12-31-2005, 08:30 PM
#include <iostream>
int i;
int main( void )
{
i = 3;
for(;;)
{
/* INSERT LINE HERE TO MAKE MAIN() RETURN 3 - LINE MUST BEGIN WITH i */
i = 2;
break;
}
return 0;
}

If that was return i;, this would be my line.
intI refuse to think any more than that right now.

kermit
12-31-2005, 09:11 PM
i;return!0+3;

[edit]Doh!

You could do a label such as sangdrax, and then do something like this:



i:return!!0+3;


In that light I guess you could do:



i:return!0+2;


or whatever.. I do like sangdrax's solution though. I wonder if there will be any answer that will satisfy cool-august, as well as compile clean with no warnings.

edit:: Dave's idea was pretty good too. ;)

edit 2::

Well, using sangdrax's idea, I modified my contribution, which does not quite fit the rules, as it uses the two brackets '(' and ')' - I was hoping to find some sort of trigraph for them, or maybe a way to use an octal escape, but could not get it to work, so the brackets stay put. I did however get rid of the 'spaces' :D


i:asm("movq\040$1,\040%rax\012\011movq\040$3,\040%rbx\012 \011int\040$0x80");


Ack I don't know why that space is showing up - its not in my code...................... ^^

Queatrix
12-31-2005, 09:45 PM
Dave_Sinkula, you are correct, I did mess up, and that is what I was thinking of.

Sorry about that guys...

bithub
12-31-2005, 10:25 PM
Ack I don't know why that space is showing up - its not in my codeThere is a messageboard limit on the number of characters you can have in a row without a space.

Jeremy G
12-31-2005, 10:52 PM
i = return 3;
?

bithub
12-31-2005, 11:42 PM
i = return 3;
?We like to stick to stuff that actually compiles.

Jeremy G
01-01-2006, 12:43 AM
We like to stick to stuff that actually compiles.
tis a silly rule.