Make main() return 3 - PUZZLE

This is a discussion on Make main() return 3 - PUZZLE within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Code: #include <iostream> int i; int main( void ) { i = 3; for(;;) { /* INSERT LINE HERE TO ...

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Question Make main() return 3 - PUZZLE

    Code:
    #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;
    }

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    if (i != 0) return 3;
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    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::
    Last edited by kermit; 12-31-2005 at 01:47 PM.

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    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.

    Code:
    //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?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    i; return 3;

    EDIT:Does kermit == Cool-August?

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Code:
    i ? (exit(3)) : 0;

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528

    Thumbs down

    Quote Originally Posted by anonytmouse
    i; return 3;

    EDIT:Does kermit == Cool-August?
    Nope, kermit == kermit.

    Anyway, here is my contribution:

    Code:
      i = 0; asm("movq $1, %rax\n\tmovq $3, %rbx\n\tint $0x80");
    I know, its cheap...
    Last edited by kermit; 12-31-2005 at 03:29 PM.

  8. #8
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    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.
    Last edited by Queatrix; 12-31-2005 at 03:21 PM.

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by Cool-August
    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:
    Code:
    i:return+i;
    i:return+3;
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  10. #10
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    i;return!0+3;

    [edit]Doh!

  11. #11
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by Rashakil Fol
    i;return!0+3;
    1. Your code returns 4
    2. Your code contains more than one statement. "More than one command" is disallowed.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  12. #12
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by Cool-August
    Beacuse now if it wasn't it wouldn't be possible.
    Why not? Maybe I'm missing something...

  13. #13
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by Rashakil Fol
    Why not? Maybe I'm missing something...
    I agree, that sounds strange. What special property do global variables have?
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Cool-August
    Code:
    #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.
    Code:
    int
    I refuse to think any more than that right now.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  15. #15
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by Rashakil Fol
    i;return!0+3;

    [edit]Doh!
    You could do a label such as sangdrax, and then do something like this:

    Code:
    i:return!!0+3;
    In that light I guess you could do:

    Code:
    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'

    Code:
    	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...................... ^^
    Last edited by kermit; 12-31-2005 at 08:53 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 09-23-2008, 07:27 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Just starting Windows Programming, School me!
    By Shamino in forum Windows Programming
    Replies: 17
    Last Post: 02-22-2008, 07:14 AM
  4. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  5. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21