19

I stumbled upon this code on Quora.

#include<stdio.h>

main(){
          int           $[]
        ={0x69,       0154,107,
     'e',0x79,0157, 117,'v',0x6a}
     ,_,__;_=__^__;__=_;while(_<(-
      (~(1<<3))+3)){(_==1<<1||_==
       -(~(1<<3))||_==11)?putchar
        (*($+(1>>1))):putchar(*(
          __++ +$)),(_==1>>1||
            _==1<<2||_==(1<<3
              )-1)?putchar
                (' '):1;
                  _++;
                    }
}

The program's output is i like you viji. It's touching, but cryptic. So I formatted it with indent to get a better idea.

main ()
{
  int $[] = { 0x69, 0154, 107,
    'e', 0x79, 0157, 117, 'v', 0x6a
  }
  , _, __;
  _ = __ ^ __;
  __ = _;
  while (_ < (-(~(1 << 3)) + 3))
    {
      (_ == 1 << 1 || _ ==
       -(~(1 << 3)) || _ == 11) ? putchar
(*($ + (1 >> 1))) : putchar (*(__++ + $)), (_ == 1 >> 1 ||
                                _ == 1 << 2 || _ == (1 << 3) - 1) ? putchar
(' ') : 1;
      _++;
    }
}

Now it's not so touching, but still a little cryptic.

So, can anybody explain how this code manages to print i like you viji?

UPDATE:

gave better names to variables $ , _ and __ and expanded ternary operators:

int a[] = { 0x69, 0154, 107, 'e', 0x79, 0157, 117, 'v', 0x6a }, x, y;

x = y ^ y;
y = x;

while (x < (-(~(1 << 3)) + 3))
  {
    if (x == 1 << 1 || x == -(~(1 << 3)) || x == 11)
      putchar (*(a + (1 >> 1)));
    else
      {
        putchar (*(y++ + a));
        if (x == 1 >> 1 || x == 1 << 2 || x == (1 << 3) - 1)
          putchar (' ');
        else
          1;
      }
    x++;
  }
Luboš Turek
  • 6,273
  • 9
  • 40
  • 50
  • 3
    try the following changes: use better variable names; simplify the constant expressions (e.g. 1<<3 - 1 is 7); expand ternary operator into `if`...`else` – M.M Feb 09 '15 at 20:49
  • I see `_ = __ ^ __;` even though `__` has not been initialized. That falls under the category of undefined behavior. – R Sahu Feb 09 '15 at 20:54
  • 6
    @RSahu technically, perhaps. Although if you know how it works, anything XOR itself is guaranteed to result in 0. – inetknght Feb 09 '15 at 20:59
  • 4
    @inetknght that only applies to initialized values; uninitialized values may be different each time they are read. [See here](http://stackoverflow.com/questions/25074180/is-aa-or-a-a-undefined-behaviour-if-a-is-not-initialized/) – M.M Feb 09 '15 at 21:04
  • these sort of programs often rely on common implementations of UB – M.M Feb 09 '15 at 21:06
  • @MattMcNabb also true and one of those subtle things which will bite someone in the ass *someday* (but probably not *today*) – inetknght Feb 09 '15 at 21:12
  • @LubošTurek one thing it does is to initialise the char array `$[]` using different methods of defining a value: octal (with leading `0`), hexadecimal (with leading `0x`), ascii character (`example 'e'`) and others are decimal, so the char array is `"ilkeyouvj"` (without a zero-terminator) which contains all the letters needed to write the message. – Weather Vane Feb 09 '15 at 21:15
  • 5
    I dont know whats with all these downvotes. This is a very on topic (and awesome) question. – David says Reinstate Monica Feb 09 '15 at 22:30

1 Answers1

21

Rewriting the code gives:

int arr1[] = { 'i', 'l', 'k', 'e', 'y', 'o', 'u', 'v', 'j'};
int i0, i1;  // Moved to new line instead of using ,

i0 = 0;  // i0 = i1 ^ i1;
i1 = i0;

while (i0 < 12)  // All strange constant like (1<<3) recalculated
{
    if (i0 == 2 || i0 == 9 || i0 == 11)   // "? :" replaced by if 
    {
            putchar(*arr1);
    }
    else
    {
        putchar (*(arr1 + i1));
        ++i1;

        if (i0 == 0 || i0 == 4 || i0 == 7)
        {
            putchar(' ');
        }
    }
    i0++;
}

Step by step description:

1) Reformat the lines, remove unnecessary spaces, insert spaces for readability

main()
{
    int $[] = {0x69, 0154,107, 'e',0x79,0157, 117,'v',0x6a} , _, __;
    _ = __^__;
    __ = _;
    while(_ < (-(~(1<<3))+3))
    {
        (_ == 1<<1 || _ == -(~(1<<3)) || _ == 11) ?
                putchar(*($ + (1>>1))) :
                putchar(*(__++ +$)), (_ == 1 >> 1 || _ == 1<<2 || _ == (1<<3)-1) ?
                        putchar(' ') : 1;
        _++;
    }
}

2) Rename variable, i.e $ to arr1, _ to i0 and __ to i1

main()
{
    int arr1[] = {0x69, 0154,107, 'e',0x79,0157, 117,'v',0x6a} , i0, i1;
    i0 = i1^i1;
    i1 = i0;
    while(i0 < (-(~(1<<3))+3))
    {
        (i0==1<<1 || i0== -(~(1<<3)) || i0 == 11) ?
                putchar(*(arr1+(1>>1))) :
                putchar(*(i1++ +arr1)), (i0 == 1 >> 1 || i0 == 1<<2 || i0 == (1<<3)-1) ?
                        putchar(' ') : 1;
        i0++;
    }
}

3) Use if-statement instead of ?: This includes breaking the comma-line into two line.

main()
{
    int arr1[] = {0x69, 0154,107, 'e',0x79,0157, 117,'v',0x6a} , i0, i1;
    i0=i1^i1;
    i1=i0;
    while(i0 < (-(~(1<<3))+3))
    {
        if (i0 == 1<<1 ||i0== -(~(1<<3)) || i0 == 11)
        {
                putchar(*(arr1+(1>>1)));
        }
        else
        {
                putchar(*(i1++ +arr1));
                if (i0 == 1 >> 1 || i0 == 1<<2 || i0 == (1<<3)-1)
                {
                        putchar(' ');
                }
                else
                {
                    1;  // This does nothing so it can be removed
                }
        }
        i0++;
    }
}

4) Recalculate number-constant to better values Examples

0x69 is the same as 'i'

1 << 1 is the same as 2

-(~(1<<3)) is the same as 9

i1 ^ i1 is the same as 0

1>>1 is the same as 0

main()
{
    int arr1[] = { 'i', 'l', 'k', 'e', 'y', 'o', 'u', 'v', 'j'} , i0, i1;
    i0 = 0;
    i1 = i0;
    while(i0 < 12)
    {
        if (i0 == 2 || i0 == 9 || i0 == 11)
        {
                putchar(*(arr1));
        }
        else
        {
                putchar(*(i1++ +arr1));

                if (i0 == 0 || i0 == 4 || i0 == 7)
                {
                        putchar(' ');
                }
        }
        i0++;
    }
}

5) Some minor final clean up

main()
{
    int arr1[] = { 'i', 'l', 'k', 'e', 'y', 'o', 'u', 'v', 'j'};  // Move i0 and
                                                                  // i1 to nextt line
    int i0, i1;

    i0 = 0;
    i1 = i0;
    while(i0 < 12)
    {
        if (i0 == 2 || i0 == 9 || i0 == 11)
        {
                putchar(*arr1);
        }
        else
        {
                putchar(*(arr1 + i1));  // Splitted into two lines
                ++i1;

                if (i0 == 0 || i0 == 4 || i0 == 7)
                {
                        putchar(' ');
                }
        }
        i0++;
    }
}

Now the code is pretty easy to read.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • Adding an explanation, the only duplicated letter is `'i'` which the code inserts (`*arr1`), or a space, at the appropriate places. – Weather Vane Feb 09 '15 at 21:40
  • 1
    Well *you* posted the answer! – Weather Vane Feb 09 '15 at 21:50
  • `i0` counts the letters (except spaces) in the output. `i1` indexes the letters in the source array. At the appropriate points, the code either outputs an `'i'` which has not appeared in the source array in sequence, by using the first array element `*arr1`, or it outputs the next letter from the source array and then, at the right places, outputs a space. – Weather Vane Feb 09 '15 at 21:55