How to understand this line of c ?

Discussion in 'C Programming' started by lnzju, Dec 31, 2005.

  1. lnzju

    lnzju Guest

    main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}
     
    lnzju, Dec 31, 2005
    #1
    1. Advertising

  2. "lnzju" <> wrote in message
    news:...
    > main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}
    >


    Don't. It's rubbish.


    --
    Ivan Budiselic
    ICQ# 104044323
    IRC: buda @ #gamer.hr@quakenet
    remove 'remove' for reply
     
    Ivan Budiselic, Dec 31, 2005
    #2
    1. Advertising

  3. lnzju

    tmp123 Guest

    lnzju wrote:
    > main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}


    Some hints:

    * Write the program with the correct tabulation
    * Take into account that usually main parameters are argc and argv, but
    they can be called with any name. And, if type is not declared K&R
    assumes int.
    * _ can be the name of a variable.
    * Split "for" in its parts: a for statement is divided in
    initialization, continue condition and update of state. The first one
    and third ones can be empty.
    * putchar returns the character written as unsigned int
    * integer applied to string returns n-th element. (this is something I
    do not know why).

    Kind regards.
     
    tmp123, Dec 31, 2005
    #3
  4. lnzju

    Broeisi Guest

    Ivan Budiselic wrote:

    > "lnzju" <> wrote in message
    > news:...
    >> main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}
    >>

    >
    > Don't. It's rubbish.
    >
    >


    Well it ain't rubbish cause it is valid C!!
    Try compiling it..

    The output will be:

    I Love You

    But this code is obfuscated.... Only the real C gurus will understand it
    I Guess.... not this novice guy...

    Broeisi
     
    Broeisi, Dec 31, 2005
    #4
  5. lnzju

    tmp123 Guest

    Even when "gcc" compiled with the flags -ansi and -pedantic, it
    produces no errors nor warnings. Only with -std=c99 it produces 3
    warnings.
     
    tmp123, Dec 31, 2005
    #5
  6. lnzju

    Flash Gordon Guest

    Broeisi wrote:
    > Ivan Budiselic wrote:
    >
    >> "lnzju" <> wrote in message
    >> news:...
    >>> main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}
    >>>

    >> Don't. It's rubbish.

    >
    > Well it ain't rubbish cause it is valid C!!


    Actually, it's not. In C main either takes no arguments or two
    arguments, not 1.

    > Try compiling it..
    >
    > The output will be:
    >
    > I Love You


    Not necessarily. It was written assuming ASCII, and not all C
    implementations use ASCII.

    > But this code is obfuscated.... Only the real C gurus will understand it
    > I Guess.... not this novice guy...


    Depends. It's not that difficult. Just format it properly and learn a
    couple of tricks and it is simple.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Dec 31, 2005
    #6
  7. "Broeisi" <> wrote in message
    news:...
    > Ivan Budiselic wrote:
    >
    >> "lnzju" <> wrote in message
    >> news:...
    >>> main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}
    >>>

    >>
    >> Don't. It's rubbish.
    >>
    >>

    >
    > Well it ain't rubbish cause it is valid C!!
    > Try compiling it..
    >
    > The output will be:
    >
    > I Love You


    Maybe on your machine. Certainly not on all.

    > But this code is obfuscated.... Only the real C gurus will understand it
    > I Guess.... not this novice guy...
    >


    There's nothing guruesque about it.

    --
    Ivan Budiselic
    ICQ# 104044323
    IRC: buda @ #gamer.hr@quakenet
    remove 'remove' for reply
     
    Ivan Budiselic, Dec 31, 2005
    #7
  8. lnzju

    pemo Guest

    "lnzju" <> wrote in message
    news:...
    > main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}


    Here it is a little clearer [hopefully?]

    #include <stdio.h>

    // x will be 1 (the name of this app would be in argv[0]) if we
    // don't invoke the app with some args.
    //
    main(x)
    {
    char c;

    // zero then!
    //
    --x;

    while(c = "J!Mpwf!Zpv\1"[x])
    {
    // No need for the -1 if we add 1 to the literal's characters...
    //
    // "I Love You\1"
    //
    c = c - 1;

    x++;

    putchar(c);
    }
    }
     
    pemo, Dec 31, 2005
    #8
  9. lnzju

    pemo Guest

    "lnzju" <> wrote in message
    news:...
    > main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}
    >


    Ah, but run it with some params, and it's dissapointing. Perhaps the
    initialiser in the for loop would have been better this way?

    for(_^=_; putchar(_++["J!Mpwf!Zpv\1"]-1); );
     
    pemo, Dec 31, 2005
    #9
  10. "lnzju" <> wrote:
    >main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}


    The two following code fragments are equivalent:

    /*-A-----------------------------------*/

    #include <stdio.h>

    int main(void)
    {
    char rubbish[] = "J!Mpwf!Zpv\1"; /* will 'print' trailing '\0' */
    char *rp = rubbish;

    while (*rp)
    {
    putchar ((*rp)-1);
    rp++;
    }
    putchar('\n'); /* not in original code */
    }

    /*-B-----------------------------------*/

    #include <stdio.h>

    int main(void)
    {
    char rubbish[] = "I Love You\0"; /* '\0' is redundant here */
    char *rp = rubbish;

    while (*rp)
    {
    putchar(*rp);
    rp++;
    }
    putchar('\n'); /* not in original code */
    }

    /*-------------------------------------*/
     
    Roberto Waltman, Dec 31, 2005
    #10
  11. lnzju

    pemo Guest

    "pemo" <> wrote in message
    news:dp6mnn$35l$...
    >
    > "lnzju" <> wrote in message
    > news:...
    >> main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}
    >>

    >
    > Ah, but run it with some params, and it's dissapointing. Perhaps the
    > initialiser in the for loop would have been better this way?
    >
    > for(_^=_; putchar(_++["J!Mpwf!Zpv\1"]-1); );


    or even something like this?

    for(_=!!_?_^_:_; putchar(_++["J!Mpwf!Zpv\1"]-1); );

    Fun on c.l.c - I love it!
     
    pemo, Dec 31, 2005
    #11
  12. lnzju

    Randy Howard Guest

    Broeisi wrote
    (in article <>):

    > Ivan Budiselic wrote:
    >
    >> "lnzju" <> wrote in message
    >> news:...
    >>> main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}
    >>>

    >>
    >> Don't. It's rubbish.
    >>
    >>

    >
    > Well it ain't rubbish cause it is valid C!!


    Not really no.

    > Try compiling it..
    >
    > The output will be:
    >
    > I Love You


    Funny, this EBCDIC box in the corner outputs:

    "Slartibartfast, system halted."

    My DSK9000 outputs:

    "My, what a silly little boy you are today."

    Immediately afterward, it caught on fire, and now you owe me a
    new one.


    > But this code is obfuscated.... Only the real C gurus will understand it
    > I Guess.... not this novice guy...


    I guess not you either.

    --
    Randy Howard (2reply remove FOOBAR)
    "The power of accurate observation is called cynicism by those
    who have not got it." - George Bernard Shaw
     
    Randy Howard, Dec 31, 2005
    #12
  13. tmp123 <> wrote:

    > Even when "gcc" compiled with the flags -ansi and -pedantic, it
    > produces no errors nor warnings. Only with -std=c99 it produces 3
    > warnings.


    You'd better try it again with -Wall. There are plenty of things
    wrong with the code regardless of which standard one applies to it.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Dec 31, 2005
    #13
  14. Broeisi <> wrote:

    > Well it ain't rubbish cause it is valid C!!
    > Try compiling it..


    test.c:1: warning: return type defaults to `int'
    test.c: In function `main':
    test.c:1: warning: implicit declaration of function `putchar'
    test.c:1: warning: control reaches end of non-void function

    Maybe YOU should have tried compiling it.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Dec 31, 2005
    #14
  15. lnzju

    Jordan Abel Guest

    On 2005-12-31, Randy Howard <> wrote:
    > Broeisi wrote
    > (in article <>):
    >
    >> Ivan Budiselic wrote:
    >>
    >>> "lnzju" <> wrote in message
    >>> news:...
    >>>> main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}
    >>>>
    >>>
    >>> Don't. It's rubbish.
    >>>
    >>>

    >>
    >> Well it ain't rubbish cause it is valid C!!

    >
    > Not really no.
    >
    >> Try compiling it..
    >>
    >> The output will be:
    >>
    >> I Love You

    >
    > Funny, this EBCDIC box in the corner outputs:
    >
    > "Slartibartfast, system halted."
    >
    > My DSK9000 outputs:
    >
    > "My, what a silly little boy you are today."
    >
    > Immediately afterward, it caught on fire, and now you owe me a
    > new one.


    Other than the one-argument main(), there's no actual undefined
    behavior, as far as I can tell. of course, the values of specific
    characters are implementation-defined, so it's not strictly conforming -
    that doesn't give the DS9K license to catch on fire, though.
     
    Jordan Abel, Jan 1, 2006
    #15
  16. lnzju

    Randy Howard Guest

    Jordan Abel wrote
    (in article <>):

    > On 2005-12-31, Randy Howard <> wrote:
    >> Broeisi wrote
    >> (in article <>):
    >>
    >>> Ivan Budiselic wrote:
    >>>
    >>>> "lnzju" <> wrote in message
    >>>> news:...
    >>>>> main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}
    >>>>>
    >>>>
    >>>> Don't. It's rubbish.
    >>>>
    >>>>
    >>>
    >>> Well it ain't rubbish cause it is valid C!!

    >>
    >> Not really no.
    >>
    >>> Try compiling it..
    >>>
    >>> The output will be:
    >>>
    >>> I Love You

    >>
    >> Funny, this EBCDIC box in the corner outputs:
    >>
    >> "Slartibartfast, system halted."
    >>
    >> My DSK9000 outputs:
    >>
    >> "My, what a silly little boy you are today."
    >>
    >> Immediately afterward, it caught on fire, and now you owe me a
    >> new one.

    >
    > Other than the one-argument main(), there's no actual undefined
    > behavior, as far as I can tell. of course, the values of specific
    > characters are implementation-defined, so it's not strictly conforming -
    > that doesn't give the DS9K license to catch on fire, though.


    How many examples of UB must be present in order to obtain such
    a license?

    BTW, where is stdio.h?

    --
    Randy Howard (2reply remove FOOBAR)
    "The power of accurate observation is called cynicism by those
    who have not got it." - George Bernard Shaw
     
    Randy Howard, Jan 1, 2006
    #16
  17. lnzju

    Jordan Abel Guest

    On 2006-01-01, Randy Howard <> wrote:
    > BTW, where is stdio.h?


    The implicit declaration of putchar is correct.
     
    Jordan Abel, Jan 1, 2006
    #17
  18. lnzju

    fidlee Guest

    Roberto Waltman wrote:
    > "lnzju" <> wrote:
    > >main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}


    can someone please explain as to what the initialization part of the
    for loop does. How does the underscore '_' play a part in the for loop?
    (in the initialization and the putchar function)?
     
    fidlee, Jan 1, 2006
    #18
  19. lnzju

    tmp123 Guest

    fidlee wrote:
    > Roberto Waltman wrote:
    > > "lnzju" <> wrote:
    > > >main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}

    >
    > can someone please explain as to what the initialization part of the
    > for loop does. How does the underscore '_' play a part in the for loop?
    > (in the initialization and the putchar function)?


    Hi,

    1) "_" can be the name of a variable. In this case, it is the name of
    the first parameter of the function main (usually called argc). If the
    program is called without arguments, it takes value 1. (to be
    practical, I will rename it to "i").

    2) This variable is used as iterator of the for statement. It is
    decremented at init (it takes value 0) and increment at for continue
    condition (++).

    3) It is used as index to char string. Taken into account that:

    a <==> *(a+b)

    The expression i["1234"] <=> *(i+"1234") <=> "1234" : i-th character
    of the string.

    Hope this answer was useful to you.

    Kind regards.
     
    tmp123, Jan 1, 2006
    #19
  20. lnzju

    fidlee Guest

    tmp123 wrote:
    > fidlee wrote:
    > > Roberto Waltman wrote:
    > > > "lnzju" <> wrote:
    > > > >main(_){for(--_;putchar(_++["J!Mpwf!Zpv\1"]-1););}

    > >
    > > can someone please explain as to what the initialization part of the
    > > for loop does. How does the underscore '_' play a part in the for loop?
    > > (in the initialization and the putchar function)?

    >
    > Hi,
    >
    > 1) "_" can be the name of a variable. In this case, it is the name of
    > the first parameter of the function main (usually called argc). If the
    > program is called without arguments, it takes value 1. (to be
    > practical, I will rename it to "i").
    >
    > 2) This variable is used as iterator of the for statement. It is
    > decremented at init (it takes value 0) and increment at for continue
    > condition (++).
    >
    > 3) It is used as index to char string. Taken into account that:
    >
    > a <==> *(a+b)
    >
    > The expression i["1234"] <=> *(i+"1234") <=> "1234" : i-th character
    > of the string.
    >
    > Hope this answer was useful to you.
    >
    > Kind regards.


    Thanks for answering this question.That brings up my next question.
    What is the significance of argv and argc in main() ?
     
    fidlee, Jan 1, 2006
    #20
    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. G Kannan
    Replies:
    1
    Views:
    1,270
    Eric J. Roode
    Oct 11, 2003
  2. Hugo
    Replies:
    10
    Views:
    1,399
    Matt Humphrey
    Oct 18, 2004
  3. kaushikshome
    Replies:
    4
    Views:
    824
    kaushikshome
    Sep 10, 2006
  4. scad
    Replies:
    23
    Views:
    1,220
    Alf P. Steinbach
    May 17, 2009
  5. Marek Stepanek
    Replies:
    12
    Views:
    440
    Peter J. Holzer
    Sep 2, 2006
Loading...

Share This Page