explain this c code...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sudhanshu
    New Member
    • Sep 2006
    • 17

    explain this c code...

    This one is a puzzle a fren has challenged me to solve.
    I tried it myself but could not get the complete answer.
    so here I am to seek ur help.


    Can anyone tell me the o/p of this c code without compiling...

    main(_){for(--_;putchar(_++["SEJBT!SPDL T\1"]-1););}

    on compiling it prints "RDIAS ROCKS".
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by sudhanshu
    This one is a puzzle a fren has challenged me to solve.
    I tried it myself but could not get the complete answer.
    so here I am to seek ur help.


    Can anyone tell me the o/p of this c code without compiling...

    main(_){for(--_;putchar(_++["SEJBT!SPDL T\1"]-1););}

    on compiling it prints "RDIAS ROCKS".
    I'm not a c compiler but that code won't compile.
    Is the puzzle supposed to be how to make it compile?

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by r035198x
      I'm not a c compiler but that code won't compile.
      Is the puzzle supposed to be how to make it compile?
      Except for just the 'main(_)' thingie it compiles and it obfuscates matters by naming
      the argc parameter '_' and by using a[i] as i[a] which is perfectly legal in C.
      It prints all the characters from that string minus1, so the last character stops
      the for loop; that program Sir, I consider baby obfuscation 101 :-P

      kind regards,

      Jos (<--- the grand DeObfuscator strikes again ;-)

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by JosAH
        Except for just the 'main(_)' thingie it compiles and it obfuscates matters by naming
        the argc parameter '_' and by using a[i] as i[a] which is perfectly legal in C.
        It prints all the characters from that string minus1, so the last character stops
        the for loop; that program Sir, I consider baby obfuscation 101 :-P

        kind regards,

        Jos (<--- the grand DeObfuscator strikes again ;-)
        Argh, I was using a c++ compile command on it.

        Comment

        • Darryl
          New Member
          • May 2007
          • 86

          #5
          Originally posted by sudhanshu
          This one is a puzzle a fren has challenged me to solve.
          I tried it myself but could not get the complete answer.
          so here I am to seek ur help.


          Can anyone tell me the o/p of this c code without compiling...

          main(_){for(--_;putchar(_++["SEJBT!SPDL T\1"]-1););}

          on compiling it prints "RDIAS ROCKS".
          Solve what? You ask can anyone tell the output of the Code and then turn around and say the output is "RDIAS ROCKS"

          here's a rewrite to make it more understandable
          Code:
          int main( int argc, const char* argv[] )
          {
               char str[] = "SEJBT!SPDLT\1";
          
               for (--argc; str[argc]-1 != 0; argc++)
               {
                     putchar(str[i]-1);
               }
          }
          of course the program only works as expected if no command line parameters is used, otherswise for every parameter you'd lose 1 char at the front and with enough parameters you'd easily go past the end bound of the string.

          Also as the last poster stated, the original code probably won't compile on most compilers, it is dependant on the compiler automatically assigning the variable "_" to an int which most compliant compilers now a days won't do but will instead produce an error.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by r035198x
            Argh, I was using a c++ compile command on it.
            The body of that ugly little thingy is still valid C++ ...

            kind regards,

            Jos (programming language lawyer par excellance ;-)

            Comment

            • Darryl
              New Member
              • May 2007
              • 86

              #7
              Originally posted by JosAH
              The body of that ugly little thingy is still valid C++ ...

              kind regards,

              Jos (programming language lawyer par excellance ;-)
              Not true, C++ currently does not allow automatic typing of variables. Maybe once the new auto keyword is added, but even then I am not sure if it can be used with parameters, only with assignment I believe.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by Darryl
                Not true, C++ currently does not allow automatic typing of variables. Maybe once the new auto keyword is added, but even then I am not sure if it can be used with parameters, only with assignment I believe.
                Of course I was only talking about the body of that little rascal (see above). The
                main() definition is all wrong of course and there are some include files missing
                etc. etc. Good ol' K&R1 C would've loved the entire thing though ;-)

                kind regards,

                Jos

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by JosAH
                  The body of that ugly little thingy is still valid C++ ...

                  kind regards,

                  Jos (programming language lawyer par excellance ;-)
                  Won't that require auto typing?
                  P.S I really need to give my c++ more time these days

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by r035198x
                    Won't that require auto typing?
                    P.S I really need to give my c++ more time these days
                    No, I was assuming an:

                    [code=cpp]
                    int _= 1;
                    [/code]

                    thingy just before the statements in that body; it doesn't matter much ;-)

                    kind regards,

                    Jos

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by JosAH
                      No, I was assuming an:

                      [code=cpp]
                      int _= 1;
                      [/code]

                      thingy just before the statements in that body; it doesn't matter much ;-)

                      kind regards,

                      Jos
                      Of course.
                      B.T.W I can now see your avatar (or lack of it) on the leader board.

                      Comment

                      • weaknessforcats
                        Recognized Expert Expert
                        • Mar 2007
                        • 9214

                        #12
                        Check out Obfuscated C and Other Mysteries by Don Libes (Wiley & Sons 1993).

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by weaknessforcats
                          Check out Obfuscated C and Other Mysteries by Don Libes (Wiley & Sons 1993).
                          Even better: check the IOCCC site (International Obfuscated C Code Contest);

                          that was so much fun in the K&R1 days. I think there's even an old entry from
                          my sick brain in there (I could be wrong though).

                          K&R1 C was the best language for those purposes. C++ has too many rules
                          for that and Java is a sissy language when it comes to obfuscation ;-)

                          kind regards,

                          Jos

                          edit: there! I found an old entry; it didn't make it as a winner though:

                          [code=c]
                          main(o,O0)char* *O0;{int OO,O;O=--o?atoi(O0[!0]):!
                          0;for(o=((OO=O* O)-O+!0+!0)>>!0;OO ;o+=((--OO%O)?-!
                          0:((((o-!0)%O)?O:0)+!0) )-(((o-!0)%O)?O:0)){pr intf
                          ("\n%*d "+!!(OO%O),!0<< !0<<!0,o+=(o<!0 )?O*O:0);}}
                          [/code]

                          It generates magic squares of odd size and it doesn't use any memory even
                          proportional to n (the odd number). Cute eh? ;-)

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #14
                            Originally posted by r035198x
                            Of course.
                            B.T.W I can now see your avatar (or lack of it) on the leader board.
                            So I noticed; btw what do you think of my fine avatar?

                            kind regards,

                            Jos;-)

                            Comment

                            Working...