is this prog stdc?

Discussion in 'C Programming' started by Heinrich Pumpernickel, Sep 10, 2007.

  1. is this prog stdc?


    no compiler warnings w/ gcc and lcc-win32 and prints "unix" here ,
    but how does it wrk???


    #include <stdio.h>
    int main(void)
    {
    return printf(&__STDC__["\n%six\n"],(__STDC__)["have"]+"fun"-0x60),
    0;
    }



    --
    mfg, heinrich :)
     
    Heinrich Pumpernickel, Sep 10, 2007
    #1
    1. Advertising

  2. "Heinrich Pumpernickel" <> a écrit dans le message de
    news: ...
    > is this prog stdc?
    >
    >
    > no compiler warnings w/ gcc and lcc-win32 and prints "unix" here ,
    > but how does it wrk???
    >
    >
    > #include <stdio.h>
    > int main(void)
    > {
    > return printf(&__STDC__["\n%six\n"],(__STDC__)["have"]+"fun"-0x60),
    > 0;
    > }


    Well on those vulgar architectures, it should print unix and an end of line.
    But It is not strictly portable: when run on a DS9000 in EBCDIC mode, it
    causes it to grow hair and hop around the room purring, can you spot why?
    Indeed switching this wonderful piece of machinery back to ASCII doesn't
    seem to fix the problem: it just stopped the purring. Should I take it to
    the vet or is there a genuine flaw in this program?

    --
    Chqrlie.
     
    Charlie Gordon, Sep 10, 2007
    #2
    1. Advertising

  3. Heinrich Pumpernickel

    Army1987 Guest

    On Sun, 09 Sep 2007 19:43:06 -0700, Heinrich Pumpernickel wrote:

    > is this prog stdc?
    >
    >
    > no compiler warnings w/ gcc and lcc-win32 and prints "unix" here ,
    > but how does it wrk???
    >
    >
    > #include <stdio.h>
    > int main(void)
    > {
    > return printf(&__STDC__["\n%six\n"],(__STDC__)["have"]+"fun"-0x60),
    > 0;
    > }

    __STDC__ is 1.
    a is defined as *(a + i).
    Arrays, including string literals, evaluate to the address of
    their first member.
    In ASCII, which gcc and lcc-win32 use, 'a' is 97, in hexadecimal
    that's 0x61.

    --
    Army1987 (Replace "NOSPAM" with "email")
    If you're sending e-mail from a Windows machine, turn off Microsoft's
    stupid “Smart Quotes†feature. This is so you'll avoid sprinkling garbage
    characters through your mail. -- Eric S. Raymond and Rick Moen
     
    Army1987, Sep 10, 2007
    #3
  4. Heinrich Pumpernickel

    Richard Bos Guest

    "Charlie Gordon" <> wrote:

    Why are you cross-posting _all_ your replies to comp.std.c? Even when
    they don't belong here?

    Richard
     
    Richard Bos, Sep 10, 2007
    #4
  5. "Richard Bos" <> a écrit dans le message de news:
    4all.nl...
    > "Charlie Gordon" <> wrote:
    >
    > Why are you cross-posting _all_ your replies to comp.std.c? Even when
    > they don't belong here?


    3 cross posts does not make for _all_ replies.

    I did post this code sniplet to comp.std.c because it raises a question that
    belongs here more than in comp.lang.c: the obfuscated expression contains
    pointer arithmetic that computes an address beyond the end of a character
    string constant. most optimizing compilers will turn (0x61 + "fun" - 0x60)
    into ("fun" + 1), but is it strictly correct or is UB invoked ?

    I apologize if this has already been discussed here before, but I think it
    is very much on topic.

    --
    Chqrlie.
     
    Charlie Gordon, Sep 10, 2007
    #5
  6. Charlie Gordon wrote:

    > pointer arithmetic that computes an address beyond the end of a character
    > string constant. most optimizing compilers will turn (0x61 + "fun" -
    > 0x60)
    > into ("fun" + 1), but is it strictly correct or is UB invoked ?
    >
    > I apologize if this has already been discussed here before, but I think
    > it
    > is very much on topic.


    No, it's not. If you asked something like "is it intentional that the
    current wording of the standard forbids that?", then, it would be on topic.
    But you ask: "With the current wording of the standard, is it UB?".

    It's off-topic.I set followup to comp.lang.c.

    The answer is yes, it's UB, because it does arithmetic outside of array
    boundaries, which is UB by 6.5.6p8. The fact that optimizing compilers
    *might* make the program behave correctly is irrelevant.

    comp.lang.c is about standard C.
    comp.std.c is about the C standard.

    --
    You can contact me at <>
     
    André Gillibert, Sep 10, 2007
    #6
  7. Army1987 wrote:
    > In ASCII, which gcc and lcc-win32 use,


    This is a bit misleading, and not necessary to the rest of your message. gcc
    will default to the character encoding of the host operating system, and
    allows for this default to be overridden using command-line options.
     
    Harald van =?UTF-8?B?RMSzaw==?=, Sep 10, 2007
    #7
  8. Charlie Gordon wrote:
    > ... most optimizing compilers will turn (0x61 + "fun" - 0x60)
    > into ("fun" + 1), but is it strictly correct or is UB invoked ?


    It violates a semantic requirement of the C standard.
     
    Douglas A. Gwyn, Sep 10, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -intl.com
    Replies:
    1
    Views:
    371
    Martin Gregorie
    Oct 22, 2006
  2. Avner
    Replies:
    1
    Views:
    561
    Larry I Smith
    Oct 15, 2005
  3. Avner
    Replies:
    0
    Views:
    522
    Avner
    Oct 15, 2005
  4. Ben Bacarisse

    Re: Random 64 bit number in stdc

    Ben Bacarisse, Aug 7, 2013, in forum: C Programming
    Replies:
    2
    Views:
    197
    Eric Sosman
    Aug 7, 2013
  5. Re: Random 64 bit number in stdc

    , Aug 7, 2013, in forum: C Programming
    Replies:
    59
    Views:
    634
    Tim Rentsch
    Sep 2, 2013
Loading...

Share This Page