Thread: What does this do?

  1. #1
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659

    What does this do?

    Just a bit of fun
    Code:
    #include <stdio.h>
    
    int main ( void ) {
        printf,"%d",32;
        return 0;
    }
    - does it compile?
    - does it run?
    - what's the output?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Yes, yes, and nothing.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You should have used invisible text or something, so as not to spoil things for the rest of us . . .

    http://www.eskimo.com/~scs/cclass/int/sx4db.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    EDIT: Ok, so I wasn't supposed to compile it, sorry. I thought maybe it was some experiment or something. I guess I could do like dwks said and color my text invisible.
    Last edited by swoopy; 07-11-2007 at 09:09 PM.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I was going to mention the warnings, but you beat me to it.

    It's kind of like doing this.
    Code:
    #include <stdio.h>
    
    int main(void) {
        printf;
        "&#37;d";
        32;
        return 0;
    }
    so you get three warnings about statements that do nothing.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dwks View Post
    You should have used invisible text or something, so as not to spoil things for the rest of us . . .
    Yeah. Kinda thoughtless, sorry.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    S'ok. swoopy did exactly the same thing, after all.

    What would make it more interesting would be if you used the return value.
    Code:
    int x = printf, "&#37;d", 32;
    I'm sure many people know that printf() returns the number of characters printed (but who ever uses that value?), so you might think that x is set to 2. But no, it's 32.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    After your first post, I thought maybe it outputted a space, so I double checked, and it did not.

  9. #9
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Here's some more fun:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char s[4];
        return printf(s, sprintf(s, "%s", "%*p"), printf);
    }
    - what is it printing?
    - is the same thing printed after each execution?
    - what about after recompiling?

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Answer (just guessing, I didn't compile it) in invisible text:
    - The address of printf
    - almost certainly, unless the dynamic library containing printf() was re-loaded for some reason
    - probably, unless printf was linked statically

    <end of text>

    BTW, you made me re-write my answer several times . . . very nice program.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    jhgjhg
    Last edited by robwhit; 07-11-2007 at 08:28 PM. Reason: i suck

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I agree with dwks.

  13. #13
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Well that's just witchcraft

Popular pages Recent additions subscribe to a feed