Kevin
Kevin

Reputation: 1151

Code obfuscation do not understand

I encountered the following code

#include <stdio.h>

int main(void)
{
    long long P = 1,E = 2,T = 5,A = 61,L = 251,N = 3659,R = 271173410,G = 1479296389,
              x[] = { G * R * E * E * T , P * L * A * N * E * T };
    puts((char*)x);
    return 0;
}

The case is I do not quite understand how it works,It is very confusing to me. Can someone you please explain this in detail?

edit: One more thing, how to print "Hola mundo!" ("Hello world" in Spanish) analogically?

Upvotes: 4

Views: 408

Answers (2)

M Oehm
M Oehm

Reputation: 29116

Here's a Spanish variant:

int main(void)
{
    int T=1, E=2, R=2, A=31, Q=784, L=70684, I=6590711, U=1181881,
        x[] = { T*I*E*R*R*A, Q*U*E, T*A*L };

    puts((char *) x);

    return 0;
}

Upvotes: 2

Mark Segal
Mark Segal

Reputation: 5550

Oh, this one is fun. Obviously you declare many long long variables, and one long long array of 2 cells. The array is therefore made of 16 bytes.

Given that each byte is one ASCII character, the array represents 16 characters (while the last one is probably zero). You can see that:

G * R * E * E * T = 1479296389 * 271173410 * 2 *2 * 5 = 8022916924116329800 = 
0x6F57206F6C6C6548

P * L * A * N * E * T = 1 * 251 * 61 * 3659 * 2 * 5 = 560229490 = 
0x21646C72

Given that your processor is Little Endian, the array's in-memory representation is:

48 65 6C 6C 6F 20 57 6F 72 6C 64 21 00 00 00 00

Which is Hello World!\x00\x00\x00\x00 in ASCII.

Upvotes: 9

Related Questions